UILongPressGestureRecognizer iOS 用于连续动作
UILongPressGestureRecognizer iOS for continuous action
我有一个按钮,我想要 "press down and hold" 这个按钮,这样它会一直打印 "Long Press" 直到我松开按键。
我在 ViewDidLoad 中有这个:
[self.btn addTarget:self action:@selector(longPress:) forControlEvents:UIControlEventTouchDown];
和
- (void)longPress: (UILongPressGestureRecognizer *)sender {
if (sender.state == UIControlEventTouchDown) {
NSLog(@"Long Press!");
}
}
我也试过这个:
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
lpgr.minimumPressDuration = 0.1;
lpgr.numberOfTouchesRequired = 1;
[self.btn addGestureRecognizer:lpgr];
它只打印出长按!即使我按住按钮也有一次。
谁能告诉我哪里做错了或错过了什么?
谢谢!
首先,UIButton 的 target-action 对回调仅在相应事件触发时执行 一次。
UILongPressGestureRecognizer 需要 minimumPressDuration 才能进入 UIGestureRecognizerStateBegan 状态,然后当手指移动,回调将以 UIGestureRecognizerStateChanged 状态触发。最后,UIGestureRecognizerStateEnded松开手指的状态。
您的要求是按下按钮时反复触发。 None 的顶部 class 满足您的需要。相反,您需要设置一个 重复触发定时器 按下按钮时,释放按钮时释放它。
所以代码应该是:
[btn addTarget:self action:@selector(touchBegin:) forControlEvents: UIControlEventTouchDown];
[btn addTarget:self action:@selector(touchEnd:) forControlEvents:
UIControlEventTouchUpInside | UIControlEventTouchUpOutside | UIControlEventTouchCancel];
- (void)touchBegin:(UIButton*)sender {
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(longPress:) userInfo:nil repeats:YES];
}
- (void)touchEnd:(UIButton*)sender {
[_timer invalidate];
_timer = nil;
}
- (void)longPress:(NSTimer*)timer {
NSLog(@"Long Press!");
}
顺便说一下,UIButton 和 UILongPressGestureRecognizer 都没有类型为 UIControlEvents[ 的状态=11=]
我有一个按钮,我想要 "press down and hold" 这个按钮,这样它会一直打印 "Long Press" 直到我松开按键。
我在 ViewDidLoad 中有这个:
[self.btn addTarget:self action:@selector(longPress:) forControlEvents:UIControlEventTouchDown];
和
- (void)longPress: (UILongPressGestureRecognizer *)sender {
if (sender.state == UIControlEventTouchDown) {
NSLog(@"Long Press!");
}
}
我也试过这个:
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
lpgr.minimumPressDuration = 0.1;
lpgr.numberOfTouchesRequired = 1;
[self.btn addGestureRecognizer:lpgr];
它只打印出长按!即使我按住按钮也有一次。 谁能告诉我哪里做错了或错过了什么? 谢谢!
首先,UIButton 的 target-action 对回调仅在相应事件触发时执行 一次。
UILongPressGestureRecognizer 需要 minimumPressDuration 才能进入 UIGestureRecognizerStateBegan 状态,然后当手指移动,回调将以 UIGestureRecognizerStateChanged 状态触发。最后,UIGestureRecognizerStateEnded松开手指的状态。
您的要求是按下按钮时反复触发。 None 的顶部 class 满足您的需要。相反,您需要设置一个 重复触发定时器 按下按钮时,释放按钮时释放它。
所以代码应该是:
[btn addTarget:self action:@selector(touchBegin:) forControlEvents: UIControlEventTouchDown];
[btn addTarget:self action:@selector(touchEnd:) forControlEvents:
UIControlEventTouchUpInside | UIControlEventTouchUpOutside | UIControlEventTouchCancel];
- (void)touchBegin:(UIButton*)sender {
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(longPress:) userInfo:nil repeats:YES];
}
- (void)touchEnd:(UIButton*)sender {
[_timer invalidate];
_timer = nil;
}
- (void)longPress:(NSTimer*)timer {
NSLog(@"Long Press!");
}
顺便说一下,UIButton 和 UILongPressGestureRecognizer 都没有类型为 UIControlEvents[ 的状态=11=]