iOS手势篇(四)-UILongPressGestureRecognizer详解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_18683985/article/details/83270275

打开头文件,很简单,就四个属性

@property (nonatomic) NSUInteger numberOfTapsRequired;      // Default is 0. The number of full taps required before the press for gesture to be recognized
@property (nonatomic) NSUInteger numberOfTouchesRequired __TVOS_PROHIBITED;   // Default is 1. Number of fingers that must be held down for the gesture to be recognized

@property (nonatomic) NSTimeInterval minimumPressDuration; // Default is 0.5. Time in seconds the fingers must be held down for the gesture to be recognized
@property (nonatomic) CGFloat allowableMovement;           // Default is 10. Maximum movement in pixels allowed before the gesture fails. Once recognized (after minimumPressDuration) there is no limit on finger movement for the remainder of the touch tracking

属性 默认值 说明
numberOfTapsRequired 0 识别长按手势之前需要轻触(Tap)手势识别多少次,默认不需要识别轻触手势
numberOfTouchesRequired 一根手指头 这个属性是长按手势最少需要多少个手指头同时按下才能识别的意思(多余这个数也能识别).我并没有打开View的allowMultiple Touch也能识别.感觉是bug
minimumPressDuration 0.5s 小于这个时间的话,长按手势的Action不会触发(不识别为长按手势)
allowableMovement 10PT,毕竟手放在上面完全不动是不可能的 如果超过这个数目,就算其他条件达成,长按手势的Action也不会触发

**注:**因为长按手势属于非离散手势.在Action中会有三种状态.1.识别成功.2.改变.3.识别完成.

- (IBAction)longPressGesture:(UILongPressGestureRecognizer *)sender {
    switch (sender.state) {
        case UIGestureRecognizerStateBegan: {
            ///识别成功是这个case.可以在这里写识别成功的代码
        }
            break;
        case UIGestureRecognizerStateChanged: {
			///
        }
            break;
        case UIGestureRecognizerStateEnded: {
        	///手势结束(比如手离开了屏幕)
        }
        default:
            break;
    }
}

如果识别成功就会走一次UIGestureRecognizerStateBegan.
如果,有,类似于位置改变之类的就会调用UIGestureRecognizerStateChanged(模拟器因为可以鼠标定住某点,定住的时候Action就不调用).但是实机测试的时候由于手不可能完全静止在某个点上,那么,Action就会疯狂调用…(拖动手势建议用UIPanGestureRecognizer,而不是判断长按手势的UIGestureRecognizerStateChanged,毕竟面向对象嘛).

猜你喜欢

转载自blog.csdn.net/qq_18683985/article/details/83270275