2

I am creating an NSView, which in its drawRect method creates and adds an NSImageView as a subview.

I would like to rotate this NSImageView (circleView), or [self]. So in another method, I am trying to do that:

-(void)startAnimation {
CABasicAnimation* spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
spinAnimation.toValue = [NSNumber numberWithFloat:5*2*M_PI];
spinAnimation.duration = 2;
[circleView.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
}

However, this code doesn't do anything when the method is called. Am I doing something wrong? I tried self.layer and circleView.layer but neither seem to work...

mootymoots
  • 4,545
  • 9
  • 46
  • 74

2 Answers2

2

The answer to this issue is that the NSView/NSImageView didn't have a backing layer to do the animation with.

You set this with:

[NSView setWantsLayer:YES];
mootymoots
  • 4,545
  • 9
  • 46
  • 74
1

Your're just defining the animation, you're not actually calling the animation block that performs the animation.

You need to call a block defined between:

+ beginAnimations:context:
+ commitAnimations

To actually run the animation.

Edit01: My bad. I didn't pay attention to the tags and added the answer for the iPhone API.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • Where is that for Cocoa on OS X?? NSView doesn't have that like UIView – mootymoots Feb 15 '10 at 20:07
  • addAnimation: option starts a core animation, as per http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/AnimatingLayers.html – mootymoots Feb 15 '10 at 20:29