
长按手势事件:
长按按钮1S后改变按钮颜色:
// 长按事件
#import "ViewController.h"
@interface ViewController (){
UIButton *myBtn;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
myBtn = [[UIButton alloc]initWithFrame:CGRectMake(, , , )];
myBtn.backgroundColor = [UIColor orangeColor];
[myBtn setTitle:@"开始按钮" forState:UIControlStateNormal];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];//初始化一个长按手势
[longPress setMinimumPressDuration:];//设置按多久之后触发事件
[myBtn addGestureRecognizer:longPress];//把长按手势添加给按钮
[self.view addSubview:myBtn];
}
-(void)longPressAction:(UILongPressGestureRecognizer*)sender{
// UIGestureRecognizerStatePossible,按钮state的各种枚举值
// UIGestureRecognizerStateBegan,
// UIGestureRecognizerStateChanged,
// UIGestureRecognizerStateEnded,
// UIGestureRecognizerStateCancelled,
// UIGestureRecognizerStateFailed,
// UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
if (sender.state == UIGestureRecognizerStateBegan) {
myBtn.backgroundColor = [UIColor greenColor];//当状态为Began时,触发事件(修改btn的背景色)
}
}
@end