有没有什么办法可以在UIView
上持续按下通知?即我用一根手指持续按UIView
,并且我想在该持续时间内一次又一次地继续调用特定的方法。在iOS的UIView上持续按下手势
我试过UILongPressGestureRecognizer
但它只是得到通知开始,结束,移动等与TouchesBegan
相同。
干杯
有没有什么办法可以在UIView
上持续按下通知?即我用一根手指持续按UIView
,并且我想在该持续时间内一次又一次地继续调用特定的方法。在iOS的UIView上持续按下手势
我试过UILongPressGestureRecognizer
但它只是得到通知开始,结束,移动等与TouchesBegan
相同。
干杯
你可以做的是,使用UILongPressGestureRecognizer和NSTimer的组合功能。
以下代码应符合您的要求。
@property (strong, nonatomic) NSTimer *timer;
- (void) viewDidLoad
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[aView addGestureRecognizer: longPress];
}
- (void) longPress:(UILongPressGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan)
{
self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(callMethod) userInfo:nil repeats:YES];
[self.timer fire];
}
if (gesture.state == UIGestureRecognizerStateEnded)
{
[self.timer invalidate];
}
}
- (void) callMethod
{
//will be called continuously within certain time interval you have set
}
火NSTimer
在touchesBegan
打电话要选择和touchesEnded
方法
无效!在TouchesBegan
启动一个定时器,当你已经达到了期望的时间量进行选择(长按)。 在TouchesEnded
使计时器无效以防止执行选择器。
我还设置了一个额外的标志检测“fingerReleased”: 设置fingerReleased = NO
上TouchesBegan
和fingerReleased = YES
上TouchesEnded
并提出要在执行代码:
if (!fingerReleased)
{
// Execute code
}
TouchesBegan and TouchesEnded:P – GenieWanted
@GenieWanted,编辑谢谢:-) –