长按手势(UILongPressGestureRecognizer)简介
长按手势一定需要和UIGestureRecognizer类的state属性进行配合使用,即通过不同状态state下,实现不同的功能。UIGestureRecognizer类的state属性中,常见的有:
- UIGestureRecognizerStateBegan:开始手势事件
- UIGestureRecognizerStateEnded:结束手势事件
- UIGestureRecognizerStateChanged:手势位置发生变化
- UIGestureRecognizerStateFailed:无法识别的手势
//长按手势
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.imageView addGestureRecognizer:longPressGesture];
//响应事件
-(void) longPress: (UILongPressGestureRecognizer *) gesture {
if (gesture.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按开始");
self.longPressLable.text = @"长按开始";
}else if (gesture.state == UIGestureRecognizerStateEnded){
NSLog(@"长按结束");
self.longPressLable.text = @"长按结束";
}
else {
NSLog(@"长按中");
self.longPressLable.text = @"长按中";
}
}
网友评论