未使用 NSAnimationContext 调用 CAAnimation animationDidStop

Posted

技术标签:

【中文标题】未使用 NSAnimationContext 调用 CAAnimation animationDidStop【英文标题】:CAAnimation animationDidStop not called with NSAnimationContext 【发布时间】:2012-08-17 11:42:06 【问题描述】:

我正在使用这段代码为 NSScrollView 滚动制作动画:

    [NSAnimationContext beginGrouping];
    NSClipView* clipView = [self contentView];
    NSPoint newOrigin = [clipView bounds].origin;
    newOrigin.x = page*kGalleryWidth;
    [[clipView animator] setBoundsOrigin:newOrigin];
    [NSAnimationContext endGrouping];

现在我正在尝试获取触发动画结束的事件。 我已经阅读了几行代码,使用 CAAnimation 可以轻松实现,但我做不到。

我已经尝试了以下代码:

CAAnimation *moveAnimation = [[self.contentView animationForKey:@"frameOrigin"] copy];
moveAnimation.delegate = self;
[self.contentView setAnimations:[NSDictionary dictionaryWithObject:moveAnimation forKey:@"frameOrigin"]];

(void)animationDidStop:(CAAnimation *)动画完成:(BOOL)flag

NSLog(@"STOP!");

有人可以帮助我吗?

谢谢!

【问题讨论】:

【参考方案1】:

你可以使用这个功能:

[[NSAnimationContext currentContext] setCompletionHandler:<#(void (^)(void))completionHandler#>]

 [NSAnimationContext beginGrouping];
    [[NSAnimationContext currentContext] setCompletionHandler:^
        NSLog(@"animation stop!");
    ];
    NSClipView* clipView = [self contentView];
    NSPoint newOrigin = [clipView bounds].origin;
    newOrigin.x = page*kGalleryWidth;
    [[clipView animator] setBoundsOrigin:newOrigin];
    [NSAnimationContext endGrouping];

【讨论】:

【参考方案2】:
[UIView beginAnimations:@"goback" context:nil];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationBeginsFromCurrentState:NO];
dragOperation.draggable.center =dragOperation.initialPoint;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector: @selector(animationDidStop:finished:context:)];
UIView commitAnimations];




-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
    [self.layer removeAnimationForKey:@"goback"];

【讨论】:

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

NSAnimationContext 不会为 NSButton 设置动画帧大小

Swift MacOS NSAnimationContext 不遵循其持续时间

带有布局约束和 NSAnimationContext 的动画不正确

为啥 NSAnimationContext completionHandler 不起作用(有时)?

使用NSView.layoutSubtreeIfNeeded()动画自动布局约束不适用于macOS High Sierra

调用结果未使用