2012-02-05 28 views
0

我想要這兩個方法來移動和旋轉UIView。兩種方法都可以單獨工作,但如果我旋轉,然後移動UIView它消失。touchesMoved:withEvent:和UIRotationgestureRecognizer不一起工作

- (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; 

} 

回答

2

手勢識別器優先於touchMoved,因此很難在相同視圖下使用它們。

使用UIPanGestureRecognizer代替touchMoved來處理拖動的UIView。然後,您可以得到UIPanGestureRecognizer和UIRotationGestureRecognizer通過實施

– gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 

方法,它是在UIGestureRecognizerDelegate協議中定義的相互合作。

+0

感謝您的快速幫助。將這樣做。 – Michael 2012-02-05 11:38:48

相關問題