iOS: jumping uiview when rotating via CATransform3DRotate

234 Views Asked by At

I try to rotate a uiview (height & width of 100) with an angle of 90 degrees with an own anchor point. After changing the anchor point I translate my view so that both changes cancel each other out.

When I rotate the view with (Double.pi) it animates as expected but when I change to (Double.pi/2) the view is jumping up.

    testView.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
    testView.layer.transform = CATransform3DMakeTranslation(0, -50, 0)

    UIView.animate(withDuration: 2.3, delay: 0, options: .curveLinear, animations: { () -> Void in
        var allTransofrom = self.testView.layer.transform
        var c3d = CATransform3DIdentity
        c3d.m34 = 2.5 / (2000)
        let rot = CATransform3DRotate(c3d, CGFloat(Double.pi/2), 1, 0, 0)
        allTransofrom = CATransform3DConcat(allTransofrom, rot)
        self.testView.layer.transform = allTransofrom
    }, completion: {(finished) in
    } )

full code here

1

There are 1 best solutions below

0
Carlo-Rodriguez On

I was able to found a solution by myself but I still don't know where the jump is coming from.

Instead of doing a translation via CGAffineTransform.translatedBy or CATransform3DMakeTranslation I simply adapted my NSLayoutConstraint.

testViewAnchors[0].constant = testViewAnchors[0].constant + CGFloat(50)

In my case an animation for the "translation" wasn't necessary but as it is possible to animate NSLayoutConstraints it shouldn't be a problem.