Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating by adding & removing NSLayoutConstraints, instead of adjusting Constants

I have an NSButton whose bottom is flush with its superview's, and I'd like to animate it moving up so that its top is flush with its superview's.

WWDC 2012 Session 228: Best Practices for Mastering Auto Layout mentions two ways to animate layout changes (31:16), and I'm attempting to use the CoreAnimation technique. The example below does correctly move the NSButton, but it does so instantaneously and with no 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 & remove NSLayoutConstraints and let CoreAnimation figure out how to animate the change? That seems simpler than me determining the distance between the button's old & new position, then adjusting the NSLayoutConstraint's Constant by that amount.

like image 494
John Sauer Avatar asked Nov 29 '12 16:11

John Sauer


People also ask

How do you add Animations?

Add animations and effectsSelect the object or text you want to animate. Select Animations and choose an animation. Select Effect Options and choose an effect.

What is the function of adding animation?

Adding animation simply means to add a special visual or sound effect to an object including entrance and exit effects, changes in size and colour, and even movement.

What is add animation in PowerPoint?

An animation effect is a preset visual effect that can be applied to the text or objects on a slide.

What is adding a transition?

Slide transitions are the animation-like effects that happen when you move from one slide to the next during a presentation. Add slide transitions to bring your PowerPoint presentation to life. Select the slide you want to add a transition to. Select the Transitions tab and choose a transition.


1 Answers

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

like image 141
John Sauer Avatar answered Oct 18 '22 21:10

John Sauer