NSButton rotate animation clockwise in cocoa mac application

1k views Asked by At

I am having a cocoa mac application in objective c.

When I press my NSButton, I want to rotate it clockwise with animation for some seconds.

I tried below code but not working.

CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    ani.fromValue = [NSNumber numberWithFloat:0];
    ani.toValue = [NSNumber numberWithFloat:-M_PI*2];
    [btnScan.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
    ani.duration = 2.0; // seconds
    ani.repeatCount = HUGE_VAL;
    [btnScan.layer addAnimation:ani forKey:nil];

I searched a lot but couldn't find a proper solution for this.

Can anyone help me on this, please?

1

There are 1 answers

4
NKorotkov On BEST ANSWER

Add [btnScan setWantsLayer:YES]; before you do your animation. NSView unlike UIView doesn't have a backing layer by default.

from Apple Docs:

In iOS apps, Core Animation is always enabled and every view is backed by a layer. In OS X, apps must explicitly enable Core Animation support by doing the following:

  • Link against the QuartzCore framework. (iOS apps must link against this framework only if they use Core Animation interfaces explicitly.)

  • Enable layer support for one or more of your NSView objects by doing one of the following:

    • In your nib files, use the View Effects inspector to enable layer support for your views. The inspector displays checkboxes for the selected view and its subviews. It is recommended that you enable layer support in the content view of your window whenever possible.

    • For views you create programmatically, call the view’s setWantsLayer: method and pass a value of YES to indicate that the view should use layers.