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

raulriera
  • 714
  • 10
  • 28

2 Answers2

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.

darren102
  • 2,830
  • 1
  • 22
  • 24
  • what if you want to actually animate removing and adding a constraint? – Daniel Galasko Jan 14 '15 at 13:24
  • 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 method – darren102 Jan 14 '15 at 15:25
  • interesting, perhaps a solution is to animate the view in and on completion add constraints. Thanks for the help:) – Daniel Galasko Jan 14 '15 at 15:26
  • Perfect answer! Thanks – Jure Jun 02 '15 at 08:48
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!!");
        }
    }
iDaniel89
  • 65
  • 6