我正在使用UIPanGestureRecognizer识别对象的拖动运动,它适用于以下状态:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[panGesture setMaximumNumberOfTouches:1];
[panGesture setDelegate:self];
[self.view addGestureRecognizer:panGesture];
[panGesture release];
- (void)panAction:(UIPanGestureRecognizer *)gR
{
switch (gR.state)
{
case UIGestureRecognizerStateBegan:
NSLog(@"drag began");
break;
case UIGestureRecognizerStateChanged:
NSLog(@"drag moved");
break;
case UIGestureRecognizerStateEnded:
NSLog(@"drag ended");
break;
default:
break;
}
}
现在我的问题是,是否有可能检测到用户开始拖动对象的情况,并且在某些时候已停止拖动并将对象保持在固定点?我已经检查过,当用户停止移动对象时,它停止记录“已更改”,直到用户开始移动对象或释放它(在这种情况下,它记录“已结束”).
这是一个示例日志:
2013-02-19 16:36:**01.181** Project[24201:10a03] drag began
2013-02-19 16:36:**14.004** Project[24201:10a03] drag moved
2013-02-19 16:36:14.221 Project[24201:10a03] drag moved
2013-02-19 16:36:**14.894** Project[24201:10a03] drag ended
您可以看到在开始,更改和结束之间存在一些暂停(那时对象被固定在一个点上).是否有可能发现这种情况?如果是这样,怎么样?
任何帮助都将得到诚挚的感谢,提前谢谢.
更新
我试过UILongPressGestureRecognizer:
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
[longPressGesture setDelegate:self];
[self.view addGestureRecognizer:longPressGesture];
[longPressGesture release];
-(void)handleLongPressGesture:(UILongPressGestureRecognizer *)gR
{
switch (gR.state)
{
case UIGestureRecognizerStateBegan:
NSLog(@"long press began");
break;
case UIGestureRecognizerStateCancelled:
NSLog(@"long press cancelled");
break;
case UIGestureRecognizerStateEnded:
NSLog(@"long press ended");
break;
default:
break;
}
}
还有委托方法:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
现在,两个识别器都是单独识别的.如果首先执行长按手势,则同时识别两个手势:
2013-02-19 17:19:13.382 Project[24357:10a03] long press began
2013-02-19 17:19:14.901 Project[24357:10a03] drag began
2013-02-19 17:19:14.937 Project[24357:10a03] drag moving
2013-02-19 17:19:15.025 Project[24357:10a03] drag moving
2013-02-19 17:19:16.317 Project[24357:10a03] long press ended
2013-02-19 17:19:16.317 Project[24357:10a03] drag ended
但是如果首先执行拖动手势,除非拖动结束,否则它不再识别长按:
2013-02-19 17:21:05.985 Project[24357:10a03] drag began
2013-02-19 17:21:06.001 Project[24357:10a03] drag moving
2013-02-19 17:21:06.018 Project[24357:10a03] drag moving
2013-02-19 17:21:**06.052** Project[24357:10a03] drag moving
2013-02-19 17:21:**17.786** Project[24357:10a03] drag moving
2013-02-19 17:21:17.818 Project[24357:10a03] drag moving
2013-02-19 17:21:17.851 Project[24357:10a03] drag moving
2013-02-19 17:21:19.324 Project[24357:10a03] drag ended
2013-02-19 17:21:20.388 Project[24357:10a03] long press began
2013-02-19 17:21:21.188 Project[24357:10a03] long press ended
拖动开始后我需要第二种情况来检测长按.
最佳答案 您可以在UIGestureRecognizerStateChanged之后安排NSTimer,如果它存在则使其失效,然后在计时器的选择器中执行任何操作.
if(self.myTimer)
[self.myTimer invalidate];
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:PAUSE_INTERVAL target:self selector:@selector(userPaused:) userInfo:nil repeats:NO];