简体   繁体   中英

Getting a smooth rotation using pan gesture recognizer

I am rotating a CATransform3D layer around y-axis by taking input from UIPanGestureRecognizer:

CGPoint movement = [recognizer translationInView:self];
CGFloat trackSpread = self.bounds.size.width;
CGFloat angle = (35 * abs(movement.x))/trackSpread;
transform = CATransform3DRotate(transform, degToRad(angle), 0, 1, 0);

The view rotates smoothly if the pan is done slowly ie receives continuous x values from recognizer but if the panning is at a faster pace ie the values receives from recognizer are not continuous I see jumps in the rotation (kind of like the layer is also translating a bit around its rotation point). Would what be the way to always keep the rotation smooth whether panning is done slow or fast?

Stand alone layers have implicit animations that happen automatically just by changing the property. This is what you are seeing. When continuously updating the property it looks like the layer is lagging and falling behind.

If you want to change the property without having the implicit animation you can change it within a transaction that has disabled all actions (a more general name for animations):

[CATransaction begin];
[CATransaction setDisableActions:YES]; // no animations
myLayer.transform = newTransform;
[CATransaction commit];

You can also configure the layer to never look for an animation for a given key path by adding NSNull to its actions dictionary for that key path:

myLayer.actions = @{
    @"transform": [NSNull null] // never animate "transform"
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM