0

我正在使用两个子视图。每个都将是独一无二的,并有自己的“行动”。

子视图 1 = 用户可以在视图周围拖动、旋转和缩放

子视图 2 = 当用户在屏幕上移动手指时,会在手指触摸的每个点添加图像。

我通过使用 UIPanGestureRecognizer 完成了这两项工作。我的问题是,我怎样才能将这两个动作分开?我希望能够添加一个子视图,执行所需的操作,然后当我添加另一个子视图时,防止之前的操作发生。

这是我尝试过的,这是在我的 panGesture 方法中完成的:

 for (UIView * subview in imageView.subviews)
 {
    if ([subview isKindOfClass:[UIImageView class]])
    {
        if (subview == _aImageView)
        {
            CGPoint translation = [panRecognizer translationInView:self.view];
            CGPoint imageViewPosition = _aImageView.center;
            imageViewPosition.x += translation.x;
            imageViewPosition.y += translation.y;

            _aImageView.center = imageViewPosition;
            [panRecognizer setTranslation:CGPointZero inView:self.view];
        }
        else if (subview == _bImageView)
        {
            currentTouch = [panRecognizer locationInView:self.view];

            CGFloat distance = [self distanceFromPoint:currentTouch ToPoint:prev_touchPoint];
            accumulatedDistance += distance;
            CGFloat fixedDistance = 60;

            if ([self distanceFromPoint:currentTouch ToPoint:prev_touchPoint] > fixedDistance)
            {
                [self addbImage];

                prev_touchPoint = currentTouch;
            }
        }
    }
}
4

2 回答 2

0

If you want different gesture recognition in two different views, put separate recognizers on each view.

于 2012-11-19T03:36:57.153 回答
0

通常,您希望视图控制器拥有并管理手势识别器,例如

- (void)viewDidLoad {
    self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    self.panGesture.delegate = self;
    [self.viewX addGestureRecognizer:self.panGesture];
    // repeat with other recognisers...
}

请注意,将您的控制器设置为 gestureRecognizer 的委托很重要:这使您能够处理来自视图控制器的以下委托方法(这是主要问题):

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    // handle your logic, which gestureRecognizer should proceed...
    return NO;
}

处理程序方法与此示例相同,但您可以根据需要设置自己的处理程序:

- (void)handleGesture:(UIGestureRecognizer*)gestureRecognizer {
     // handle gesture (usually sorted by state), e.g.
     // if(gesture.state == UIGestureRecognizerStateEnded) { ... }
}
于 2015-03-24T02:08:21.320 回答