问题
I'm trying to rotate the contents of an NSView
(NSButton
) in-place (I.e. from the center. I want to convert -
into |
). This can be done with setFrameCenterRotation:
… However, I'm trying to animate this, and
[[myview animator] setFrameCenterRotation: 90];
causes the control to first jump to the right, and then rotate around the bottom-left corner (0,0) back to the original location.
Some people have suggested first setting the anchorPoint
:
[[myview layer] setAnchorPoint: NSMakePoint(0.5, 0.5)];
but for some reason the animation cancels this out and it gets reset to (0, 0).
So, has anybody figured out how to animate an in-place rotation of an NSView
?
回答1:
This doesn't happen if you add the control from interface builder and rotate it using its IBOutlet. You will not have to use "setAnchorPoint:" method; "setFrameCenterRotation:" method itself will work. However the same thing happened to me when I tried to rotate a NSButton I have in my NSCell as a subview. The solution was to have an NSView as a subview in the controlView (refer the NSCell subclass below) and NSButton I require rotation as a subview of that new NSView.
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView{
if (![btnCheckBackView superview]) {
//btnCheckBackView is my NSView holding the NSButton "btnCheck"
[btnCheckBackView setFrame:NSRectFromCGRect(CGRectMake(9, cellFrame.origin.y+8, 28, 28))];
[controlView addSubview:btnCheckBackView];
if (![btnCheck superview]) {
[btnCheck setFrame:NSRectFromCGRect(CGRectMake(0, 0, 28, 28))];
[btnCheckBackView addSubview:btnCheck];
}
}
// NSLog(@"drawWithFrame");
// NSLog(@"Rect %d,%d,%d,%d",(int)cellFrame.origin.x,(int)cellFrame.origin.y,(int)cellFrame.size.width,(int)cellFrame.size.height);
// NSLog(@"controlView %d,%d,%d,%d",(int)controlView.frame.origin.x,(int)controlView.frame.origin.y,(int)controlView.frame.size.width,(int)controlView.frame.size.height);
}
来源:https://stackoverflow.com/questions/7278852/animated-rotation-of-nsview-contents