UIPanGestureRecognizer - 使用CATransform3D的translationInView

时间:2014-02-28 17:07:22

标签: ios core-animation uipangesturerecognizer catransform3d catransform3drotate

我正在尝试使用UIPanGestureRecognizer实现页面翻转动画。这是代码:

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

    CGPoint location = [recognizer locationInView:self.view];
    CGPoint translation = [recognizer translationInView:self.view];
    CGPoint velocity = [recognizer velocityInView:self.view];

    [recognizer setTranslation:CGPointZero inView:recognizer.view];

switch (recognizer.state) {
        case UIGestureRecognizerStateChanged:
        {

            self.currentTranslation = self.currentTranslation + translation.x;

            //only pan left
            if (self.currentTranslation > 0.0) {
                self.currentTranslation = 0.0;
            }


            CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
            rotationAndPerspectiveTransform.m34 = 1.0 / -2000;

            self.currentRotation = self.currentTranslation/2 * M_PI / 180.0f;
            //dont rotate past -90 degrees
            if (self.currentRotation <= -M_PI/2) {
                self.currentRotation = -M_PI/2+0.0001;
            }
            rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, self.currentRotation, 0.0f, 1.0f, 0.0f);

            float ypos = self.view.layer.position.y;

            self.view.layer.anchorPoint = CGPointMake(0, 0.5);
            self.view.layer.position = CGPointMake(0, ypos);        
            self.view.layer.transform = rotationAndPerspectiveTransform;


            break;
        }


        default:
            break;
    }

}

动画有效,当我向左拖动时页面转动。然而,即使我以稳定,缓慢的速度移动手势,页面也开始变得更快。我希望页面在触摸移动时“线性”转动,就像Flipboard应用程序一样。如何控制翻译,使其不会加速?

1 个答案:

答案 0 :(得分:3)

累积翻译。在案例UIGestureRecognizerStateChanged之后将其添加到您的代码中。

    [recognizer setTranslation:CGPointZero inView:self.view];