动画自动布局与NSPopover内容同时更改 [英] Animating Auto Layout changes concurrently with NSPopover contentSize change
问题描述
我正在尝试在弹出窗口中重现iTunes 11中可导航视图的行为。我似乎找不到一种方法来使我的动画与弹出窗口的 contentSize
更改同时发生。
I'm attempting to reproduce the iTunes 11 behavior of navigable views within a popover. I can't seem to find a way to get my animation to happen at the same time as the popover's contentSize
change happens, though.
我拥有的基本设置是具有两个子视图的自定义视图子类MyPopoverNavigationView:我希望弹出菜单在其间进行浏览的旧视图和新视图。弹出窗口的 contentViewController
具有MyPopoverNavigationView实例作为其视图
。我这样做:
The basic setup I have is a custom view subclass MyPopoverNavigationView with two subviews: the old and new views that I want the popover to navigate between. The popover's contentViewController
has a MyPopoverNavigationView instance as its view
. I do this:
// Configure constraints how I want them to show the new popover view
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *ctx) {
[ctx setDuration:0.25];
[ctx setAllowsImplicitAnimation:YES];
[self layoutSubtreeIfNeeded];
} completionHandler:nil];
据我从《自动布局WWDC 2012》视频中得知,这是推荐的动画制作方式由于约束更改而导致视图框架的更改。它可以工作,但是动画分为两个阶段:
As far as I can tell from the Auto Layout WWDC 2012 videos, this is the recommended way to animate changes to views' frames as a result of constraint changes. It works, but the animation happens in two phases:
- 首先,弹出窗口的
contentSize
将会更改,以适应我要移至的新视图(在该视图可见之前,它们会部分遮盖现有内容)。 - 第二,
- First, the popover's
contentSize
will change to accommodate the new view that I'm moving to (before that view becomes visible, so it partially obscures the existing content). - Second, the views animate as I expect, so that the constraints system I installed is satisfied.
通过设置一些断点,它看起来像 -layoutSubtreeIfNeeded
最终在弹出窗口上调用一个名为 _fromConstraintsSetWindowFrame:
的私有方法,该方法在动画组之外执行弹出窗口大小的动画。我的上下文的持续时间没有得到尊重,动画直到弹出窗口的大小更改完成才发生。
From setting some breakpoints, it looks like -layoutSubtreeIfNeeded
eventually calls a private method on the popover called _fromConstraintsSetWindowFrame:
, which does the popover size animation outside my animation group. My context's duration isn't respected, and my animations don't happen until the popover's size change is complete.
如何获取与弹出窗口的动画一起的视图
How can I get my views to animate together with the popover's size change?
推荐答案
原来的诀窍是显式设置弹出框的 contentSize
动画和完成块的属性外部。来自示例GitHub项目的相关代码段我将其汇总起来看起来像:
Turns out the trick is to explicitly set the popover's contentSize
property outside of the animation and completion blocks. The relevant snippet from the sample GitHub project I put together to figure it out looks like:
// Configure constraints for post-navigation view layout
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *ctx) {
[ctx setDuration:0.25];
[ctx setAllowsImplicitAnimation:YES];
[self layoutSubtreeIfNeeded];
} completionHandler:^{
// Tear down some leftover constraints from before the transition
}];
// Explicitly set popover's contentSize so its animation happens simultaneously
containingPopover.contentSize = postTransitionView.frame.size;
这篇关于动画自动布局与NSPopover内容同时更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!