cyclingIsBetter
cyclingIsBetter

Reputation: 17591

iOS: Autolayout doesn't work fine with animation

In my app I have a storyboard and I use "autolayout" option; The problem is if I move some UIImageView with an animation in this way:

[UIView animateWithDuration:1.0 animations:^{

    [image setCenter:CGPointMake(300,200)];
}];

when the animation finish the image return in the original position, why??? If I not check autolayout option it work fine. Can you tell me if I can manage animation with autolayout? And what's the way to do it?

Upvotes: 0

Views: 213

Answers (1)

Fogmeister
Fogmeister

Reputation: 77641

FIRST RULE OF AUTOLAYOUT! You cannot change the frame or centre of views using AutoLayout.

In order to animate a view you have to set up a constraint that you will use to animate it and then change the constraint.

i.e.

self.heightConstraint = [NSLayoutConstraint constraintWithItem:self.theView
attribute:
relatedBy:
toItem:
attribute:
multiplier:
constant:];
// set up the rest

[self.view addConstraint:self.heightConstraint];

Then to animate it...

self.heightConstraint.constant = 20;

[UIView animateWithDuration:2.0
animations:^() {
    [self.view layoutIfNeeded];
}];

Upvotes: 1

Related Questions