类目
#import <UIKit/UIKit.h>
@interface UIButton (ZJQ_button)
+ (id)zjq_initWithFrame:(CGRect)frame
target:(id)target
action:(SEL)selecor
title:(NSString *)title;
@end
#import "UIButton+ZJQ_button.h"
@implementation UIButton (ZJQ_button)
+ (id)zjq_initWithFrame:(CGRect)frame
target:(id)target
action:(SEL)selecor
title:(NSString *)title
{
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
button.frame = frame;
[button setTitle:titleforState:UIControlStateNormal];
button.layer.borderWidth =1;
[button addTarget:targetaction:selecor forControlEvents:UIControlEventTouchUpInside];
return button;
}
@end
扩展,协议
#import <UIKit/UIKit.h>
@protocol myViewDelegate <NSObject>
- (void)changColor;
@end
@interface MyView :UIView
@property(nonatomic,assign)id<myViewDelegate>delegate;
@end
#import "MyView.h"
@implementation MyView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.delegatechangColor];
}
@end
@interface MainViewController ()<myViewDelegate>
@property(nonatomic,retain)MyView *myView;
@end
...- (void)changColor{
self.myView.backgroundColor = [UIColorcolorWithRed:arc4random() %256 /255.0green:arc4random() %256 /255.0blue:arc4random() %256 /255.0alpha:1];
}
触碰
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnd:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
摇一摇- (void)motionBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)motionEnd:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)motionCancelled:(NSSet *)touches withEvent:(UIEvent *)event;