animationDidStop:finished: 未调用

Posted

技术标签:

【中文标题】animationDidStop:finished: 未调用【英文标题】:animationDidStop:finished: not called 【发布时间】:2012-04-18 07:24:39 【问题描述】:

我正在尝试使用顶部的滑动动画将视图替换为另一个视图。我有点遵循http://gentlebytes.com/2011/09/auto-layout-in-lion/ 中发布的指南,但没有 NSConstraints。

不幸的是,我的动画的委托方法 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 &lt;QuartzCore/QuartzCore.h&gt;

【讨论】:

谢谢。那成功了。一开始我怀疑我需要这些图层,但由于动画仍然有效,我认为它们并不是真正需要的。

以上是关于animationDidStop:finished: 未调用的主要内容,如果未能解决你的问题,请参考以下文章

animationDidStop:完成:委托方法没有被调用

属性动画(显示动)

尝试使用 CAAnimation 和 CATextLayer 滑动文本