16

我有一个图像类型的对象,我正在使用 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];  

}

我该怎么做?

4

3 回答 3

23

UIGestureRecognizers 有一个 enabled 属性。文档:

禁用手势识别器,使其不接收触摸。默认值为是。如果在手势识别器当前正在识别手势时将此属性更改为 NO,则手势识别器将转换为取消状态。

编辑:

只需将 enabled 属性设置为 NO。

gestureRecognizer.enabled = NO;
于 2011-11-10T11:43:43.117 回答
15

当您需要阻止 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;
    }
}
于 2012-10-31T13:11:38.127 回答
5

设置 pangesture.delegate=self,并实现委托方法

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
     //return NO when you reach the frame
}
于 2012-10-31T15:41:43.447 回答