问题
I'm having a bit of trouble with getting CoreAnimation to rotate an NSView about its center point. All I want to do is rotate an NSView while keeping its center point static so that the object appears to rotate in place.
The way I chose to do this involves the use of the animator
property in NSView. The code is as follows:
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:1.0];
[[view animator] setFrameCenterRotation:45.0];
[NSAnimationContext endGrouping];
Where view
is the NSView that I am trying to rotate. The result of this code makes the object abruptly shift to the right somewhat then does the rotation. The final result is correct, but the animation doesn't look right because of the movement it makes in the beginning. I'm assuming that this is because the way that setFrameCenterRotation:
works is that it shifts the frame origin to the view's center point first, then makes the rotation.
What am I doing wrong here? Is there a better way, perhaps, to rotate an NSView about the center? Thanks a lot!
回答1:
First make sure NSView layer backed (Core Animation enabled), animator proxy would otherwise animate without CA. Do this with -setWantsLayer:YES, or by checking the corresponding option for that view in IB.
Second, set the layer anchor point to 0.5, 0.5:
view.layer.anchorPoint = CGPointMake(0.5,0.5);
Third, you may need direct layer interaction if you can't get what you want, but first try with AppKit API's, as it preserves mouse events, etc.
来源:https://stackoverflow.com/questions/5446779/coreanimation-setframecenterrotation-doesnt-animate-properly