美文网首页功能模块IOS三人行
UILongPressGestureRecognizer长按手势

UILongPressGestureRecognizer长按手势

作者: 面试小集 | 来源:发表于2015-12-19 20:42 被阅读3080次

    长按手势(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 = @"长按中";
     }
    }
    

    相关文章

      网友评论

        本文标题:UILongPressGestureRecognizer长按手势

        本文链接:https://www.haomeiwen.com/subject/iytmhttx.html