0

嗨,我UIPanGestureRecognizer在我的项目中使用过。我用下面的代码来处理UIPanGestureRecognizer

- (void)handlePan:(UIPanGestureRecognizer *)recognizer {

      CGPoint translation = [recognizer translationInView:self];
      CGFloat newX = MIN(recognizer.view.frame.origin.x + translation.x, self.frame.size.width - recognizer.view.frame.size.width);
      CGRect newFrame = CGRectMake( newX,recognizer.view.frame.origin.y, recognizer.view.frame.size.width, recognizer.view.frame.size.height);
      recognizer.view.frame = newFrame;
      [recognizer setTranslation:CGPointZero inView:self];
}

在此处输入图像描述

我的项目中有两个视图,一个是父视图(红色视图),另一个是子视图(绿色视图)。我的要求是我需要在父视图中平移子视图。我的handlePan方法工作正常,但唯一的问题是它允许平移超出左侧的限制。它在右侧工作得很好。我需要更改代码请帮助我。

4

1 回答 1

0

你应该检查UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch当您不希望手势识别器接收到触摸事件时,实现并返回 NO。

此方法在 touchesBegan:withEvent: 之前调用,在手势识别器上调用新触摸。

于 2013-01-23T07:55:25.480 回答