有什么方法可以让用户连续按下UIView
来得到通知吗?即我一直用一根手指按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
}
关于ios - 在iOS中的UIView上连续按下手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21904265/