2012-04-18 65 views
0

我試圖用頂部的滑動動畫替換另一個視圖。我有點遵循http://gentlebytes.com/2011/09/auto-layout-in-lion/這裏發佈的指南,但沒有NSConstraints。animationDidStop:finished:not called

不幸的是,委託方法animationDidStop:finished:我的動畫在動畫結束時沒有被調用。

這裏是我的代碼:

- (void)slideViewControllerFromTop:(NSViewController *)controller { 
[self replacePlaceholderViewWith:controller block:^(NSView *placeholder, NSView *currentView, NSView *newView) { 
    NSRect initialFrameNewView = NSMakeRect(placeholder.bounds.origin.x, 2*placeholder.bounds.size.height - newView.frame.size.height - DISTANCE_FROM_TOP, placeholder.bounds.size.width, newView.frame.size.height); 
    [newView setFrame:initialFrameNewView]; 
    NSRect finalFrameNewView = NSMakeRect(placeholder.bounds.origin.x, placeholder.bounds.size.height - newView.frame.size.height - DISTANCE_FROM_TOP, 0.91*placeholder.bounds.size.width, newView.frame.size.height); 

    NSRect finalFrameCurrentView = NSMakeRect(currentView.frame.origin.x, currentView.frame.origin.y-placeholder.bounds.size.height, currentView.frame.size.width, currentView.frame.size.height); 

    CABasicAnimation *animation = [CABasicAnimation animation]; 
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
    [animation setDelegate:self]; 
    [animation setDuration:0.5]; 
    animation.removedOnCompletion = YES; 

    [newView setAnimations:[NSDictionary dictionaryWithObject:animation forKey:@"frame"]]; 
    [currentView setAnimations:[NSDictionary dictionaryWithObject:animation forKey:@"frame"]]; 

    [placeholder addSubview:newView]; 
    [NSAnimationContext beginGrouping]; 
    [[newView animator] setFrame:finalFrameNewView]; 
    [[currentView animator] setFrame:finalFrameCurrentView]; 
    [NSAnimationContext endGrouping]; 
}]; 
} 

- (void)replacePlaceholderViewWith:(NSViewController *)controller block:(GBPlaceholderReplaceBlock)handler { 
    if (controller == self.currentViewController) return; 
    self.previousViewController = self.currentViewController; 
    self.currentViewController = controller; 
    NSView *placeholderView = self.contentView; 
    NSView *currentView = self.contentView.subviews.lastObject; 
    NSView *newView = self.currentViewController.view; 
    handler(placeholderView, currentView, newView); 
} 

- (void)animationDidStop:(CABasicAnimation *)anim finished:(BOOL)flag { 
    if (!flag || self.contentView.subviews.count < 2) return; 
    [[self.contentView.subviews objectAtIndex:0] removeFromSuperview]; 
    self.previousViewController = nil; 
} 

回答

1

嘗試更換這兩個

[newView setAnimations:[NSDictionary dictionaryWithObject:animation forKey:@"frame"]]; 
[currentView setAnimations:[NSDictionary dictionaryWithObject:animation forKey:@"frame"]]; 

[newView.layer addAnimation:animation forKey:@"frame"]; 
[currentView.layer addAnimation:animation forKey:@"frame"]; 

而且不要忘了在類文件的頂部添加#import <QuartzCore/QuartzCore.h>

+0

謝謝。那就是訣竅。開始時我懷疑我需要這些圖層,但是由於動畫無論如何都起作用,我認爲它們並不是真的必要。 – Jacopo 2012-04-18 09:53:36

+0

@Jacopo歡迎:) – Kjuly 2012-04-18 09:55:20