Skip navigation.
 
mlRe: Layer Backed Views and CoreAnimation neither animate nor stay in place
FROM : Brian Christensen
DATE : Fri Jun 27 19:16:02 2008

On Jun 27, 2008, at 08:56, Chilton Webb wrote:

> (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.


I'm surprised it doesn't work as intended. Maybe it's a bug in the 
replaceSubview:with: method. If you change your testSwap: method to 
the following it should maintain the view ordering:

- (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];
   
   NSView *viewToReplace = [[self subviews] objectAtIndex:1];
   [[self animator] addSubview:svx positioned:NSWindowAbove 
relativeTo:viewToReplace];
   [[viewToReplace animator] removeFromSuperview];
   
   [svx release];
}

It looks like you'll have to add this as well:

- (void)awakeFromNib
{
   [self setWantsLayer:YES];
}

Your transition probably won't work as intended with this solution 
though (it works fine with the default fade animation, however), so to 
get the effect you want you may need to position the new view outside 
the visible boundaries of the superview and then use something like 
this to perform the transition:

[NSAnimationContext beginGrouping];
[NSAnimationContext setDuration:0.5];

[[svx animator] setFrameOrigin:destinationFrameOrigin];
[[viewToReplace animator] 
setFrameOrigin:somewhereOutsideVisibleBoundaries];

[NSAnimationContext endGrouping];

[viewToReplace performSelector:@selector(removeFromSuperview) 
withObject:nil afterDelay:0.5];

(I'm not sure that the transition you specified would work with 
replaceSubview:with: either. It seems to perform the animation on the 
entire superview, not just the subview being changed.)

/brian

Related mailsAuthorDate
mlLayer Backed Views and CoreAnimation neither animate nor stay in place Chilton Webb Jun 27, 14:56
mlRe: Layer Backed Views and CoreAnimation neither animate nor stay in place Brian Christensen Jun 27, 19:16
mlRe: Layer Backed Views and CoreAnimation neither animate nor stay in place Uli Kusterer Jul 5, 17:34
mlRe: Layer Backed Views and CoreAnimation neither animate nor stay in place David Duncan Jul 5, 20:03