Animate constraints using Facebook pop?
Asked Answered
L

2

7

I was able to animate a constraint change using

[UIView animateWithDuration:0.5
                      delay:0.5
     usingSpringWithDamping:0.7
      initialSpringVelocity:0.7
                    options:0
                 animations:^{
                     [self.closeButton layoutIfNeeded];
                 } completion:NULL];

But I was under the impression that this could also be done using the Facebook POP library. Can anyone point me in the right direction to finding out how?

Thank you

Lehman answered 1/9, 2014 at 19:43 Comment(0)
A
19

In this case you want to animate an NSLayoutConstraint you can do the following with POP and it will animate the constraint.

constraint // this is an NSLayoutConstraint that is applied to some view

POPSpringAnimation *layoutAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayoutConstraintConstant];
layoutAnimation.springSpeed = 20.0f;
layoutAnimation.springBounciness = 15.0f;
layoutAnimation.toValue = @(value to go too);
[constraint pop_addAnimation:layoutAnimation forKey:@"detailsContainerWidthAnimate"];

The main property to use is the kPOPLayoutConstraintConstant as shown above.

Apolitical answered 24/9, 2014 at 18:57 Comment(3)
what if you want to actually animate removing and adding a constraint?Groundhog
You probably would not want the POP framework but instead use the standard UIView animate methods by adding and removing the constraints then calling the setLayoutIfNeeded in the animation block for the UIView animateWith methodApolitical
interesting, perhaps a solution is to animate the view in and on completion add constraints. Thanks for the help:)Groundhog
W
0

Here is another example using spring animation...

    -(void)shakeViewConstraint:(NSLayoutConstraint*)constraint{

        POPSpringAnimation *springStart = [POPSpringAnimation animationWithPropertyNamed:kPOPLayoutConstraintConstant];

        springStart.springSpeed = 0.5;
        springStart.springBounciness = 0.3;
        springStart.fromValue = @(50);
        springStart.toValue = @(25);
        springStart.velocity = @600;
        springStart.delegate = self; //Using Delegates as handlers

        [constraint pop_addAnimation:springStart forKey:@"animationUniquekey"];

        //Using Blocks as handlers
        [springStart setCompletionBlock:^(POPAnimation* animation, BOOL finished) {

            if (finished)
                [constraint pop_removeAnimationForKey:@"animationUniquekey"];
        }];
    }

    #pragma mark - POP Animation Delegates

    -(void)pop_animationDidStart:(POPAnimation *)anim{

        //NSLog(@"POP ANIM STARTED!!");

    }

    -(void)pop_animationDidStop:(POPAnimation *)anim finished:(BOOL)finished{

        //NSLog(@"POP ANIM STOPPED!!");

        if (finished) {

            //NSLog(@"POP ANIM FINISHED!!");
        }
    }
Woolsey answered 18/9, 2015 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.