您的位置:首页 > 产品设计 > UI/UE

UILongPressGestureRecognizer的介绍

2015-11-14 11:48 363 查看
首先UILongPressGestureRecognizer是继承自UIGestureRecognizer
所有的手势都是继承自UIGestureRecognizer

UIView *longPressView = [[UIView
alloc]init];

longPressView.backgroundColor = [UIColor
redColor];

longPressView.frame =
CGRectMake(100,
200, 100, 100);

[self.view
addSubview:longPressView];

创建一个长按的手势
这个方法是UIGestureRecognizer的方法

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer
alloc]initWithTarget:self
action:@selector(longPress:)];

为这个view添加手势

[self.view
addGestureRecognizer:longPress];

longPress.minimumPressDuration =
4;

The minimum period fingers must press on the view for the gesture to be recognized.

设置长按手势
最低多少秒后触发的时间,默认的是0.5秒
这个数值是按秒计算的

longPress.numberOfTouchesRequired =
1;

The number of fingers that must be pressed on the view
for the gesture to be recognized

设置几个手指来触发这个长按手势
默认是1
一般都是1
若为0
则不触发

longPress.numberOfTapsRequired =
0;

The number of taps on the view required for the gesture to be recognized

在这个view上手势需要识别轻敲击的次数
设置为1
就以为着连续快速的点击1次离开屏幕在迅速的点击这个view长按
不放到minimumPressDuration
才会触发手势的方法

默认是0
就是按住不放 不需要离开屏幕

longPress.allowableMovement =
20;

The maximum movement of the fingers on the view before the gesture fails

这个属性不是手势开始之后滑动的最大距离
只要UIGestureRecognizerStateBegan
你可以滑动任意大的距离

这是属性是刚刚开始触摸view的时候到UIGestureRecognizerStateBegan这时时间内
最大的滑动距离

默认是10

这个方法是触发这个手势后进入的方法 有兴趣的孩纸可以玩玩

-(void)longPress:(UILongPressGestureRecognizer *)longPress{

if (longPress.state ==
UIGestureRecognizerStateBegan) {

NSLog(@"longPressBegan");

}else
if (longPress.state ==
UIGestureRecognizerStateChanged){

NSLog(@"longPressChange");

}else
if (longPress.state ==
UIGestureRecognizerStateEnded){

NSLog(@"end");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: