Animated rotation of NSView contents

徘徊边缘 提交于 2019-12-30 11:07:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!