dimanche 2 août 2015

iOS 7+ scale animation of new subview with autolayout

I'm trying to create "pop-in" animation (view height and width should go from 0 to some value) but only thing I've achieved so far is view transiting from left side of the container view to the final position (some code bellow).

Is it even possible to do this animation using only constraints and UIView's animateWithDuration:... method and if it's possible what should I know about it?

Here's some code I use to add new view into container view. I'm using PureLayout for setting up constraints but that shouldn't be problem.

//lastView is not nil
UIImageView *lastView = [containerView lastObject];

//Add view to the subview
[containerView addSubview:newView];

//Setup constraints for new view
[newView autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lastView withOffset:LABEL_PADDING_HOR];
[newView autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
[newView autoMatchDimension:ALDimensionWidth toDimension:ALDimensionHeight ofView:newView];


//Here I've tried to set height to 0 then layout containerView and then set constraint to the final size but that doesn't work

//Animating this constraint
viewHeightConstraint = [newView autoSetDimension:ALDimensionHeight toSize:50];

// These 2 lines will cause -[updateViewConstraints] to be called again on this view, where the constraints will be adjusted to the new state
[containerView setNeedsUpdateConstraints];
[containerView updateConstraintsIfNeeded];

[UIView animateWithDuration:5.0
                      delay:0.0
     usingSpringWithDamping:0.6
      initialSpringVelocity:0
                    options:0
                 animations:^{
                              [containerView layoutIfNeeded]; // this is what actually causes the views to animate to their new layout
                             }
                 completion:^(BOOL finished) {
                             }];

Thank you for any advice!

Aucun commentaire:

Enregistrer un commentaire