Layer Backed Views and CoreAnimation neither animate nor stay in place
Layer Backed Views and CoreAnimation neither animate nor stay in place
- Subject: Layer Backed Views and CoreAnimation neither animate nor stay in place
- From: Chilton Webb <email@hidden>
- Date: Fri, 27 Jun 2008 05:56:25 -0700
Hi,
I am absolutely confused regarding something simple in layer backed view animation, and it's been haunting me for many months now. So I have constructed a sample project that illustrates the 3 problems I run into most frequently. I'm hoping someone will point to some place in the docs I've missed, and explain why I'm having what I see as simple problems, so often.
Here's the completed, zipped sample project. It's 63k.
http://homepage.mac.com/chilton/.Public/SubViewDepthTest.zip
The symptoms:
(1) If I specify that I want anything other than the main view to have a layer, that view will instantly 'pop' to the foreground in the window. However, its actual view remains in the same place in the view hierarchy.
(2) If I set [self setWantsLayer:YES] anywhere except where it is currently uncommented, the animations don't work at all. The window performs the update instantly, and without animating it! This seems very wrong. I would expect that I could set it at the end of my initialization at least, after I've built up my other layers, so that the tree would populate correctly.
If I do that, no animation, but at least the view is replaced in the right stacking order.
(3) Even when everything else is working right, the first time I perform my animation method, the animation does not work. Instead, it quickly swaps out the old view with the new one, and displays it in the foreground, on top of all other views, even if all other views are layer backed. This is obviously *not* 'replacing' the view in the order I want. After that, it animates properly, but on top of the other views instead of behind them.
All I want is to animate the swapping out of the middle layer. What on Earth am I doing wrong?
The very simple sample project is above, and the two main classes used are below. The first does all the work, the second is just there to look pretty.
ANY SUGGESTIONS/CRITICISMS/COMMENTS GREATLY APPRECIATED!!!
Thank you,
-Chilton Webb
// SubViewDepthTest.m
// SubViewDepthTest
//
// Created by Chilton Webb on 6/26/08.
//
// (1) If I specify that I want anything other than the main view to have a layer,
// that view will instantly 'pop' to the foreground in the window. However,
// its actual view remains in the same place in the view hierarchy.
//
// (2) If I set [self setWantsLayer:YES] anywhere except where it is currently uncommented,
// the animations don't work at all. The window performs the update instantely,
// without animating it.
//
// (3) The first time we perform this method, this animation does not work,
// it quickly swaps out the old view with the new one, and displays it in the foreground.
//
#import "SubViewDepthTest.h"
@implementation SubViewDepthTest
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
CATransition *transition = [CATransition animation];
[transition setType:kCATransitionPush];
[transition setSubtype:kCATransitionFromLeft];
NSDictionary *ani = [NSDictionary dictionaryWithObject:transition forKey:@"subviews"];
[self setAnimations:ani];
// (2) If I set this here, the animations don't work.
// [self setWantsLayer:YES];
// Set up our views
ColoredSubView *sv1 = [[ColoredSubView alloc] initWithFrame:NSMakeRect(0,0,frame.size.width,frame.size.height)];
[sv1 setColor:[NSColor redColor]];
[self addSubview:sv1];
ColoredSubView *sv2 = [[ColoredSubView alloc] initWithFrame:NSMakeRect(50,50,frame.size.width,frame.size.height)];
[sv2 setColor:[NSColor greenColor]];
[self addSubview:sv2];
ColoredSubView *sv3 = [[ColoredSubView alloc] initWithFrame:NSMakeRect(100,100,frame.size.width,frame.size.height)];
[sv3 setColor:[NSColor blueColor]];
[self addSubview:sv3];
// (2) If I set this here, the animations don't work, but the stacking order is right.
// [self setWantsLayer:YES];
// Clean up
[sv1 release];
[sv2 release];
[sv3 release];
}
return self;
}
- (IBAction) testSwap:(id) sender
{
// (2) If I set this anywhere else, the animations don't work.
[self setWantsLayer:YES];
// (3) See notes above
ColoredSubView *svx = [[ColoredSubView alloc] initWithFrame:NSMakeRect(50,50,200,200)];
[svx setColor:[self anothercolor]];
[[self animator] replaceSubview:[[self subviews] objectAtIndex:1] with: svx];
[svx release];
}
- (NSColor *) anothercolor
{
static unsigned i;
NSColor *color;
i++;
if (i == 1) {
color = [NSColor blackColor];
} else if (i == 2) {
color = [NSColor purpleColor];
} else if (i == 3) {
color = [NSColor yellowColor];
} else if (i == 4) {
color = [NSColor orangeColor];
} else if (i == 5) {
color = [NSColor brownColor];
}
if (i> 4) i = 0;
return color;
}
- (void)drawRect:(NSRect)rect {
// Drawing code here.
}
@end
//////////////////
// The ColoredSubView view is an NSView subclass that tacks on the setColor method and then draws it. Nothing fancy.
@implementation ColoredSubView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void) setColor:(NSColor *) color
{
_color = [color retain];
}
- (void)drawRect:(NSRect)rect {
// Drawing code here.
[_color set];
[NSBezierPath fillRect:rect];
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden