检查NSAnimationContext runAnimationGroup是否已取消或成功 [英] Check if NSAnimationContext runAnimationGroup cancelled or succeeded

查看:236
本文介绍了检查NSAnimationContext runAnimationGroup是否已取消或成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对视图进行动画处理(通过显示视图),然后我需要发布通知(动画完成后)。无论应用采用何种设计方式,当视图被隐藏时(通过动画)都会发出另一条通知。因此,基本上我有一个 showView和 hideView方法。每个操作都这样:

I'm animating a view (by revealing it) after which I need to post a notification (once the animation completes). However the way the app's designed, there's another notification sent out when the view is hidden (via animation). So essentially I have a 'showView' and a 'hideView' method. Each do something like so:

    [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
      [context setDuration: 0.25];

      [[myView animator] setAlphaValue: 0];
    } completionHandler:^{
       // post notification now
    }];

问题是,在后台的另一个线程中,我有一个执行健全性的定期计时器检查以查看是否需要隐藏该视图(如果不需要的话)。这是一种激进的方法,但有必要,因为应用程序的许多组件都可以响应不同的事件和场景。因此,有时我会遇到竞赛条件,即用户单击了 showView,但与此同时,我们的线程之一认为该视图应立即隐藏。

The problem is that in another thread in the background, I have a periodic timer which performs a 'sanity check' to see if the view needs to be hidden if it's not needed. It's an aggressive approach but necessary as many components of the app respond to different events and scenarios. Due to this I at times get a 'race condition' where the user has clicked to 'showView' but at the same time one of our threads feel the view should be immediately hidden.

当两者都在主线程上发布通知时,该应用将无限期地挂在SpinLock中。如果我能够确定上面的动画块是否被取消(即在同一视图上执行的另一个动画块),则可以完全避免这种情况。在这种情况下,我不会发布通知,这样就不会强制检查。

When both post a notification on the main thread, the app hangs indefinitely in a SpinLock. I could completely avoid this situation if I was able to figure out if the animation block above was 'cancelled' (i.e. another animation block executed on the same view). In such situations I would not post the notification, which would then not force a check.

长话短说:我需要能够检查'completionHandler'是否为在动画成功结束或被取消后调用。我知道在iOS中这是可行的,但我找不到在OS X中执行此操作的任何方法。请提供帮助。

Long story short: I need to be able to check if the 'completionHandler' was called after animation successfully ended or if it was cancelled. I know in iOS this is possible but I can't find any way to do this in OS X. Please help.

推荐答案

我遇到了类似的问题。我使用 currentAnimationUUID 实例变量解决了这个问题。

I encountered a similar problem. I solved it by using a currentAnimationUUID instance variable.

NSUUID *animationUUID = [[NSUUID alloc] init];
self.currentAnimationUUID = animationUUID;

[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
    // set properties to animate
} completionHandler:^{
    BOOL isCancelled = ![self.currentAnimationUUID isEqual:animationUUID];
    if (isCancelled) {
        return;
    }

    self.currentAnimationUUID = nil;

    // do stuff
}];

快速版本:

let animationUUID = UUID()
currentAnimationUUID = animationUUID

NSAnimationContext.runAnimationGroup({ context in
    // set properties to animate
}, completionHandler: {
    let isCancelled = self.currentAnimationUUID != animationUUID
    guard !isCancelled else {
        return
    }

    self.currentAnimationUUID = nil

    // do stuff
})

这篇关于检查NSAnimationContext runAnimationGroup是否已取消或成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆