【问题标题】:UIPanGestureRecognizer - translationInView with CATransform3DUIPanGestureRecognizer - 带有 CATransform3D 的 translateInView
【发布时间】:2014-02-28 17:07:21
【问题描述】:

我正在尝试使用 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 应用程序一样。如何控制翻译不加速?

【问题讨论】:

    标签: ios core-animation uipangesturerecognizer catransform3d catransform3drotate


    【解决方案1】:

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

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

    【讨论】:

    • 请注意,我在上面的代码中已经有了这个。我认为随着角度变大,旋转效果看起来更加极端,这更像是一个问题。我认为翻译本身是线性的。我只需要弄清楚如何抑制旋转。
    • @csoul 我认为你的 self.currentTranslation 是累积的。请尝试使用 translation.x 而不是添加到 CATransform3DRotate 中的 self.currentTranslation。我相信这是问题所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2012-09-27
    • 2023-03-05
    • 2012-03-20
    • 2010-10-08
    • 2012-04-14
    相关资源
    最近更新 更多