2016-03-07 43 views
0

我試圖在使用​​的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:^{ 
    }]; 
}); 

我在做什麼錯? 在此先感謝!

回答

0

Dispatch_after將執行主隊列上的塊。 可能沒有它,你試圖做動畫不在主線程。

此外,你可以檢查動畫將工作,如果你用dispatch_async替換dispatch_after

+0

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

+0

你能分享一下你的代碼嗎? –