【问题标题】:How to combine two rotations at two different anchor points to transform UIView如何结合两个不同锚点的两个旋转来变换 UIView
【发布时间】:2014-05-09 16:31:31
【问题描述】:

我想首先围绕它的左边缘旋转我的 UIView,然后围绕它的右边缘旋转。第一次旋转应该产生开门效果,一旦完成,第二次旋转应该开始。当第二次旋转完成时,效果应该就像视图在 z 平面中被推下一样。转换是手动动画的一部分,当然应该是无缝的。

凭直觉,我猜不可能使用锚点并连接发生在不同锚点的 2 个旋转(但如果我错了,请纠正我)。因此,我没有使用锚点,而是使用矩阵组合来产生“旋转”的效果。

第一次旋转很容易,但我不知道如何附加第二次。也许在应用第二次旋转之前,我应该在 y 坐标上向右平移并在 z 坐标上向下移动(使用 cos(90 - angle) * view.width 之类的东西)以到达右边缘的位置第一次旋转结束。然而我所做的尝试并没有成功。任何想法? 这是我到目前为止第一次轮换的内容:

-(void) transform:(float) percentage{

    CATransform3D transform = CATransform3DIdentity;

    //here I get the angle for the 1st rotation (percentage 0 -> angle 0; percentage 0.5 -> angle 45°
    float angle = [self getFirstRotationAngle: percentage];
    CATransform3D translateToRotateAt = CATransform3DMakeTranslation(myView.frame.size.width / 2.0, 0.0, 0.0);
    CATransform3D rotation1 = CATransform3DIdentity;
    rotation1.m34 = 1.0 / -500;
    rotation1 = CATransform3DRotate(rotation1, angle, 0.0f, 1.0f, 0.0f);
    CATransform3D translateBackToRotateAt = CATransform3DMakeTranslation(-myView.frame.size.width / 2.0, 0.0, 0.0f);

    transform = CATransform3DConcat(translateToRotateAt, rotation1);
    transform = CATransform3DConcat(transform , translateBackToRotateAt);

    if (percentage > 0.5){
        angle = [self getSecondRotationAngle: percentage];

        // here I should get the second part of the transformation and concatenate it to the first part
    }

    myView.layer.transform = transform;
}

【问题讨论】:

    标签: ios iphone cocoa-touch rotation transformation


    【解决方案1】:

    这种方法可以为你做一个转换,你的学位赋予它

    func makeTransform(horizontalDegree: CGFloat, verticalDegree: CGFloat, maxVertical: CGFloat,rotateDegree: CGFloat, maxHorizontal: CGFloat) -> CATransform3D {
        var transform = CATransform3DIdentity
               
        transform.m34 = 1 / -500
        
        let xAnchor = (horizontalDegree / (2 * maxHorizontal)) + 0.5
        let yAnchor = (verticalDegree / (-2 * maxVertical)) + 0.5
        let anchor = CGPoint(x: xAnchor, y: yAnchor)
        
        setAnchorPoint(anchorPoint: anchor, forView: self.imgView)
        let hDegree  = (CGFloat(horizontalDegree) * .pi)  / 180
        let vDegree  = (CGFloat(verticalDegree) * .pi)  / 180
        let rDegree  = (CGFloat(rotateDegree) * .pi)  / 180
        transform = CATransform3DRotate(transform, vDegree , 1, 0, 0)
        transform = CATransform3DRotate(transform, hDegree , 0, 1, 0)
        transform = CATransform3DRotate(transform, rDegree , 0, 0, 1)
        
        return transform
    }
    
    func setAnchorPoint(anchorPoint: CGPoint, forView view: UIView) {
        var newPoint = CGPoint(x: view.bounds.size.width * anchorPoint.x, y: view.bounds.size.height * anchorPoint.y)
        var oldPoint = CGPoint(x: view.bounds.size.width * view.layer.anchorPoint.x, y: view.bounds.size.height * view.layer.anchorPoint.y)
        
        newPoint = newPoint.applying(view.transform)
        oldPoint = oldPoint.applying(view.transform)
        
        var position = view.layer.position
        position.x -= oldPoint.x
        position.x += newPoint.x
        
        position.y -= oldPoint.y
        position.y += newPoint.y
        
        print("Anchor: \(anchorPoint)")
        
        view.layer.position = position
        view.layer.anchorPoint = anchorPoint
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多