简体   繁体   中英

Rotate NSButton clockwise with animation

I'm trying to rotate an NSButton clockwise until a user manually interrupts it. Here's the code I'm using to accomplish this. I know it used to work at some point. Any idea how to fix it? Thanks in advance!


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

Your code works fine for me as long as I set reloadButton.wantsLayer = YES;


From Enabling Core Animation Support in Your App

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

Try this

    CABasicAnimation *rotation;
    rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotation.fromValue = [NSNumber numberWithFloat:0];
    rotation.toValue = [NSNumber numberWithFloat:(M_PI)];
    rotation.duration = 0.2; // Speed
    rotation.repeatCount = 1; // Repeat forever. Can be a finite number.
    [yourButton.layer addAnimation:rotation forKey:@"Spin"];
    [yourButton.layer setZPosition:100];
    yourButton.transform = CGAffineTransformMakeRotation(M_PI_2);

You can change/set duration, repeatCount and toValue to your convenient.

[EDIT1]

After seeing that this is for an NS versus a UI button, there are a couple of options:

1) Use an NSTimer and the proper rotation routine for you OSX version (see the link below)

http://digerati-illuminatus.blogspot.com/2009/09/how-do-you-rotate-nsbutton-nstextfield.html

2) If you are using OSX 10.5 or above CoreAnimation was supported, and the below should actually be supported for NSButtons.

Wiki Link

http://en.wikipedia.org/wiki/Core_Animation

[ORIGINAL] Try this code instead:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:5000000];
CGAffineTransform rr=CGAffineTransformMakeRotation(5000000);
reloadButton.transform=CGAffineTransformConcat(reloadButton.transform, rr);
[UIView commitAnimations];

Here is a link to a SO question that shows both methods

UIView Rotation

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