手势识别器,奇怪的行为(乘数效应)iOS

时间:2012-08-10 07:07:07

标签: iphone ios xcode ipad uigesturerecognizer

我正在尝试向视图添加手势识别器。当我添加pan(用于移动)和pinch(用于缩放)时,我会得到一些奇怪的行为。

在视图(恰好包含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 个答案:

答案 0 :(得分:1)

您要求在gestureView中进行翻译,这将为您提供缩放的翻译。如果将gestureView缩小到50%,则沿x轴移动10个像素将使得translate64值中的x = 20,因为“在外部”的10个像素在按比例缩小(“更高分辨率”)的手势视图上加倍。 / p>

然后将其应用于gestureView的框架(中心只是一种方便的方法,将框架设置为center.x - frame.size.width/2, center.y - frame.size.height/2, frame.size),这意味着它受到比例的影响。

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