2012-08-10 44 views
0

我想向視圖添加手勢識別器。當我添加pan(移動)和pinch(縮放)時,我會遇到一些奇怪的行爲。手勢識別器,奇怪的行爲(乘數效應)iOS

在縮放視圖(恰好包含imageView)後,平移手勢識別器似乎將移動的效果倍增。或者它仍然採用原來的比例,所以這個舉動比它應該更快?

下面是方法的代碼移動和規模:

//ivars for gesture recognizers/transforms 
CGFloat scale; 
CGFloat rotation; 
CGPoint translatedPoint; 
CGFloat _lastScale; 
CGFloat _lastRotation; 
CGFloat _firstX; 
CGFloat _firstY; 

-(void)scale:(id)sender { 
    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) { 
     _lastScale = 1.0; 
    } 
    scale = 1.0 - (_lastScale - [(UIPinchGestureRecognizer*)sender scale]); 
    CGAffineTransform currentTransform = gestureView.transform; 
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale); 
    [gestureView setTransform:newTransform]; 
    _lastScale = [(UIPinchGestureRecognizer*)sender scale]; 
    NSLog(@"gestureView origin x: %f, y: %f", gestureView.frame.origin.x, gestureView.frame.origin.y); 
    NSLog(@"gestureView center x: %f, y: %f",[gestureView center].x, [gestureView center].y); 
    NSLog(@"gestureView scale x: %f, scale y: %f", gestureView.frame.size.width, gestureView.frame.size.height); 

} 

-(void)move:(id)sender { 
    translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:gestureView]; 
    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) { 
     _firstX = [gestureView center].x; 
     _firstY = [gestureView center].y; 
    } 
    translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y); 
    [gestureView setCenter:translatedPoint]; 
    NSLog(@"gestureView origin x: %f, y: %f", gestureView.frame.origin.x, gestureView.frame.origin.y); 
    NSLog(@"gestureView center x: %f, y: %f",[gestureView center].x, [gestureView center].y); 
} 

下面是該項目: owolf.net/uploads/StackOverflow/GestureRecognizerTest.zip

感謝您的閱讀!

回答

1

您正在翻譯gestureView內部,這會給你縮放翻譯。如果gestureView縮小到50%,則沿x軸移動10像素會使translatePoint值中的x = 20,因爲「外部」的10個像素在按比例縮小(「更高分辨率」)gestureView時翻倍。

然後,您將其應用於gestureView的框架(中心只是一個簡便的方法,它將框架設置爲center.x - frame.size.width/2, center.y - frame.size.height/2, frame.size),這意味着它受到比例的影響。

您可能想要做的是更改translationInView:調用以查看gestureView.superview,而不是gestureView本身。