我有一个使用UIPanGestureRecognizer移动的图像类型的对象,当对象到达某个帧时我需要停止识别UIPanGestureRecognizer。
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[templatePhotoPlaceholderView addGestureRecognizer:panRecognizer];
-(void)move:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint translatedPoint = [gestureRecognizer translationInView:templatePhotoPlaceholderView];
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
_firstX = [imageview center].x;
_firstY = [imageview center].y;
}
translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);
//NSLog(@" Move center point :%@", NSStringFromCGPoint(translatedPoint));
[imageview setCenter:translatedPoint];
}
我怎么能这样做?
答案 0 :(得分:23)
UIGestureRecognizers具有已启用的属性。文档:
禁用手势识别器,使其无法接收触摸。该 默认值为YES。如果在a时将此属性更改为NO 手势识别器当前正在识别手势,手势 识别器转换为已取消状态。
编辑:
只需将enabled属性设置为NO。
gestureRecognizer.enabled = NO;
答案 1 :(得分:12)
当您需要阻止UIPanGestureRecognizer识别手势时,您只需将此代码行(如jbat100所述)放在-(void)move:(UIPanGestureRecognizer *)gestureRecognizer
中:
gestureRecognizer.enabled = NO;
在此行之后,您的gestureRecognizer状态设置为"UIGestureRecognizerStateCancelled"
然后只需在您的-(void)move:(UIPanGestureRecognizer *)gestureRecognizer
函数中添加几行:
if ([gestureRecognizer state] == UIGestureRecognizerStateCancelled) {
gestureRecognizer.enabled = YES;
}
您将能够使用手势识别器
修改强>:
这是代码段:
- (void)move:(UIPanGestureRecognizer *)gestureRecognizer {
BOOL cancelPanGesture = YES;
if (cancelPanGesture) {
/*
After this line gesture recognizer will be disabled, state will be UIGestureRecognizerStateCancelled
and this method (move:) will fire one more time.
*/
gestureRecognizer.enabled = NO;
}
if (gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
gestureRecognizer.enabled = YES;
}
}
答案 2 :(得分:5)
设置pangesture.delegate = self,并实现委托方法
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
//return NO when you reach the frame
}