Resize animation with auto-layout and "jumpy" focus rings

时间:2018-05-28 18:40:44

标签: cocoa autolayout core-animation

I'm working on a macOS App that uses auto-layout in a view hierarchy with layer backed views. The App uses an NSStackView with several subviews that each feature a collapse/unfold button to resize the respective subview. Resizing of subviews is implemented by adding and removing layout constraints and an animation context is used to animate the size change. I implemented this as demonstrated on WWDC 2013, Session 213, starting at about minute 29:

@objc func disclosureToggeled(_ sender : Any) {
    if isCollapsed {
        self.addConstraint(collapseConstraint)
    }
    else {
        self.removeConstraint(collapseConstraint)
    }

    NSAnimationContext.runAnimationGroup({ context in
        context.allowsImplicitAnimation = true
        self.window?.layoutIfNeeded()
    })
}

The resize animation works as expected.

My problem: if I trigger a resize animation and a subview containing a focussed UI element is animated into a new position its focus ring immediately jumps from its starting to its final position while the UI element itself animates correctly.

Any idea what I am doing wrong?

1 个答案:

答案 0 :(得分:0)

我认为,这与该视频中讨论的问题有关(跳窗调整大小)。视频中还讨论了解决方案

  1. 通过constraint.animator.constant访问器或
  2. 显式设置动画约束常量(而不是添加/删除约束)
  3. 首先分别对所讨论的约束进行动画处理,然后对窗口框架进行动画处理,同时将约束优先级保持在窗口调整大小优先级以下,以便对约束进行动画处理不会调整窗口的大小。

我发现,如果可以通过简单的常量更改来描述约束更改,则方法1更简单。