Animation by adding and removing NSLayoutConstraints instead of setting constants

I have an NSButton whose bottom is flush with its supervisor, and I would like to revive it by moving up so that its top is flush with the add-in.

WWDC 2012 Session 228: β€œBest Practices for Mastering Auto Car Deals” mention two ways to animate layout changes (31:16), and I'm trying to use the CoreAnimation technique. The example below correctly moves NSButton, but it does it instantly and without animation.

[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] ; 

Can I add and remove NSLayoutConstraints and let CoreAnimation figure out how to bring the change back to life? It seems simpler than me, determining the distance between the old and new button position, and then setting the NSLayoutConstraint constant to this amount.

+6
source share
1 answer

After adding button.superview.wantsLayer = YES , the above example enlivens.

+7
source

All Articles