【问题标题】:Animating by adding & removing NSLayoutConstraints, instead of adjusting Constants通过添加和删除 NSLayoutConstraints 来制作动画,而不是调整常量
【发布时间】:2012-11-17 18:18:06
【问题描述】:

我有一个 NSButton,它的底部与其父视图齐平,我想动画它向上移动,使其顶部与其父视图齐平。

WWDC 2012 Session 228: Best Practices for Mastering Auto Layout 提到了两种动画布局更改的方法 (31:16),我正在尝试使用 CoreAnimation 技术。下面的示例确实正确地移动了 NSButton,但它是立即移动的,并且没有动画。

[button.superview removeConstraint:pointerToBottomSpaceConstraint] ;
NSArray* topSpaceConstraintArray = [NSLayoutConstraint constraintsWithVisualFormat: @"V:|[button]"
                                                                           options: 0
                                                                           metrics: nil
                                                                             views: NSDictionaryOfVariableBindings(button)] ;
[button.superview addConstraints:topSpaceConstraintArray] ;
[NSAnimationContext runAnimationGroup:^(NSAnimationContext* context) {
    context.duration = 2 ;
    context.allowsImplicitAnimation = YES ;
    [button.superview layoutSubtreeIfNeeded] ;
} completionHandler:nil] ;

我可以添加和删除 NSLayoutConstraints 并让 CoreAnimation 弄清楚如何为更改设置动画吗?这似乎比我确定按钮的旧位置和新位置之间的距离要简单,然后将 NSLayoutConstraint 的常量调整该数量。

【问题讨论】:

    标签: core-animation osx-mountain-lion autolayout nslayoutconstraint


    【解决方案1】:

    添加button.superview.wantsLayer = YES后,上面的例子动画正确。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 2020-10-19
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多