使用NSAnimationContext动画自动布局仅适用于dispatch_after块

问题描述:

我试图在使用​​的Mac应用程序中动画约束更改,但动画只有在嵌入dispatch_after块内时才能正常工作。使用NSAnimationContext动画自动布局仅适用于dispatch_after块

在结果,我有这样的代码:

__weak typeof(self) weakSelf = self; 
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
    [weakSelf layoutSubtreeIfNeeded]; 
    [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){ 
     __strong typeof(weakSelf) strongSelf = weakSelf; 
     context.duration = animated ? 0.3 : 0.; 
     context.allowsImplicitAnimation = YES; 
     strongSelf.expanded = NO; 
     strongSelf.collapsingConstraint.priority = 900; 
     [strongSelf layoutSubtreeIfNeeded]; 
    } completionHandler:^{ 
    }]; 
}); 

我在做什么错? 在此先感谢!

Dispatch_after将执行主队列上的块。 可能没有它,你试图做动画不在主线程。

此外,你可以检查动画将工作,如果你用dispatch_async替换dispatch_after

+0

我已经检查了很多次。它在主线上。 'dispatch_async(dispatch_get_main_queue(),^ {' - no animation ... – Bambuh

+0

你能分享一下你的代码吗? –