touchesMoved:withEvent:和UIRotationgestureRecognizer不能一起工作

时间:2012-02-05 11:19:38

标签: ios uigesturerecognizer uitouch

我正在尝试这两种方法来移动和旋转UIView。两种方法分开工作,但如果我旋转然后移动UIView它就会消失。

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {


CGRect rect = self.aView.frame;

UITouch *touch = [touches anyObject];

CGPoint pPoint = [touch previousLocationInView:self.view];
CGPoint cPoint = [touch locationInView:self.view];

float deltaX = cPoint.x - pPoint.x;
float deltaY = cPoint.y - pPoint.y;

rect.origin.x = rect.origin.x + deltaX;
rect.origin.y = rect.origin.y + deltaY;

self.aView.frame = rect;

}
- (void)rotate:(UIRotationGestureRecognizer *) recognizer {

    CGFloat rotation = angle + recognizer.rotation;

    NSLog(@"%f", angle * 180 / M_PI);

    self.aView.transform = CGAffineTransformMakeRotation (rotation);

    if (recognizer.state == UIGestureRecognizerStateEnded) 
        angle = rotation;

}

1 个答案:

答案 0 :(得分:2)

手势识别器优先于touchMoved,因此使用相同的视图很难使用它们。

使用UIPanGestureRecognizer而不是touchMoved来处理拖动UIView。然后,您可以通过实施

让UIPanGestureRecognizer和UIRotationGestureRecognizer相互合作。
– gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

方法,在UIGestureRecognizerDelegate协议中定义。