6

我有一个 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 的常量调整该数量。

4

1 回答 1

7

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

于 2012-12-01T23:57:44.883 回答