small medium large xlarge

22 Oct 2009, 14:20
Stephen Riehm (3 posts)


I like to tweak sample code to see if I have really understood everything. In this regard I thought it would be nice if the rotating image faded a bit while it was rotating. To this end I added the following to Animationtypes/GroupAnimation/GroupAnimationView.m:

- (CABasicAnimation *)opacityAnimation {
    CABasicAnimation *opacity =
    [CABasicAnimation animationWithKeyPath:@"alphaValue"];
    opacity.fromValue = [NSNumber numberWithFloat:1.0f];
    opacity.toValue = [NSNumber numberWithFloat:0.2f];
    return opacity;

and then added [self opacityAnimation] to the group.animations array.

It works as expected if it’s on its own or in combination with the frameAnimation (KeyFrame), but as soon as frameRotation and opacityAnimation are grouped together, the image doesn’t rotate at all but just slides around a bit leaving nasty trailing pixels instead.

What could possibly be going on here?

Thanks in advance,


22 Oct 2009, 15:45
Bill Dudney (917 posts)

Hi Steve,

The animations keypath is ‘alphaValue’ it should be ‘opacity’.

let me know if that clears it up.

25 Oct 2009, 12:49
Stephen Riehm (3 posts)

Hi Bill,

Thanks for the quick response!

Sadly, it didn’t work, I just get the error: [<NSImageView 0x106aba0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key opacity.

I found the alphaValue key by chasing the class hierarchy from NSImageView to NSView, to wit:

NSView alphaValue: "This method returns the value of the opacity property of the receiver’s layer."

Here’s some screenshots of the effect when using alphaValue, frame and frameRotation together:




01 Nov 2009, 21:13
Stephen Riehm (3 posts)

Time to post a radar?

02 Nov 2009, 12:52
Bill Dudney (917 posts)

Hi Steve,

Sorry for being late to respond. I’ve been traveling a lot…

I misunderstood your original post. I’ve been playing with it for the last 15 min and I’m seeing the same thing you are seeing, sounds like a radar to me… Feel free to post the example as a reproducible case.

03 Nov 2009, 18:33
Sebastian (2 posts)


I tried to do just that, adding opacity to a non CALayer. It worked as long as the picture is not rotated. I also used the alphaValue key…

For alphaValue to work, you need to use layer backing, which you can turn on for your NSView in IB (command-2 in the inspector and check your NSView layer). However, doing this will have an impact on the animation timing (this I haven’t figured out yet).

All in all, opacity is best when used with CALayers. Later in the book there are examples of how to use it (PhotoPop example).

Hope this helps.

Sets up the animations using two dictionary entries:

- (IBAction)makeFast:(id)sender {
  CABasicAnimation *frameAlphaAnimation = [CABasicAnimation animation];
  [frameAlphaAnimation setDuration:0.1f];
	CABasicAnimation *frameSizeAnimation = [CABasicAnimation animation];
	[frameSizeAnimation setDuration:0.1];
  NSDictionary *animations = [NSDictionary dictionaryWithObjectsAndKeys:
                              frameAlphaAnimation, @"alphaValue",
							  frameSizeAnimation, @"frameSize", nil];
  [myView.mover setAnimations:animations];

This will trigger both animations (alpha + zooming):

- (void)move { 	
  NSPoint target;
	NSSize zoomed;
	CGFloat alpha;
	NSRect viewFrameLarge = [super frame];
	NSRect viewFrameSmall = NSInsetRect(viewFrameLarge, 100, 100);
	if(isRight) {
		zoomed = viewFrameLarge.size;
		alpha = 1.0f;
	} else {
		zoomed = viewFrameSmall.size;
		alpha = 0.0f;
	[[mover animator] setFrameSize:zoomed];
	[[mover animator] setAlphaValue:alpha];
You must be logged in to comment