UIView层在围绕非中心锚点旋转时移动

时间:2015-09-26 20:37:11

标签: ios uiview catransform3d

我有一个UIView,其中包含另一个UIView。当覆盖视图被轻敲时,我希望它“下降”,这是我们围绕其底部中心点围绕其x轴设置动画的地方。

我这样实现了这个:

private func showCategories(animated: Bool)
{
    UIView.animateWithDuration(2,
        delay: 0,
        options: [.CurveEaseIn],
        animations: {

            self.setAnchorPoint(CGPoint(x: 0.5, y: 1), forView: self.titleView)

            var rotationAndPerspectiveTransform = CATransform3DIdentity
            rotationAndPerspectiveTransform.m34 = 1 / -500
            rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, CGFloat(-179 * M_PI / 180), 1, 0, 0);

            self.titleView.layer.transform = rotationAndPerspectiveTransform;
        }) { (success) -> Void in

            //
    }
}

我找到了一个解决方案,它说改变锚点会移动视图,所以要修复它,你需要移动它来考虑锚点和变换。它提供了这段代码:

private func setAnchorPoint(anchorPoint: CGPoint, forView view: UIView)
{
    var newPoint = CGPoint(x: view.bounds.width * anchorPoint.x, y: view.bounds.height * anchorPoint.y)
    var oldPoint = CGPoint(x: view.bounds.width * view.layer.anchorPoint.x, y: view.bounds.height * view.layer.anchorPoint.y)

    newPoint = CGPointApplyAffineTransform(newPoint, view.transform);
    oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);

    var position = view.layer.position;

    position.x -= oldPoint.x;
    position.x += newPoint.x;

    position.y -= oldPoint.y;
    position.y += newPoint.y;

    view.layer.position = position;
    view.layer.anchorPoint = anchorPoint;
}

然而,这仍然不适合我。我使用它错了吗?

P.s,我意识到animated参数尚未被考虑用于...这是出于调试目的。

1 个答案:

答案 0 :(得分:0)

好的,所以将setAnchor:forView:方法移动到动画块之前。

我的坏。