Question

I want to let the user rotate one of my views. When I do rotation by code - it looks best with this code:

#define D2R(x) (x * (M_PI/180.0))
myLayer.transform = makePerspectiveTransform();
myLayer.transform = CATransform3DRotate(topPart.transform, D2R(65), 1, 0, 0);
 CATransform3D makePerspectiveTransform()
{
 CATransform3D transform = CATransform3DIdentity;
 transform.m34 = 1.0 / 800;
 return transform;
}

But I want the user to be able to control the transform (where the max value would be D2R(65).

How can I do that?

Update

This is the pan recogniser code:

- (void)rotate:(UIPanGestureRecognizer*)gesture 
{
    CGPoint velocity = [gesture velocityInView:self.view];
    if (velocity.x > 0 && degree < 45) {
        degree += 1;
        myLayer.transform = CATransform3DRotate(myLayer.transform, D2R(2/degree), 1, 0, 0);
    }
}
Was it helpful?

Solution

Your code is both increasing the rotation amount you add to the transform, and applying the rotation to the already-rotated view transform. So each time it starts from where it left off and moves more.

Don't apply a rotation to the existing transform. When you begin your animation, save the starting transform into an instance variable. Then apply the rotation value you calculate from the pan gesture to the ORIGINAL transform and save that as the view's transform.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top