0

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?

1

1 Answer 1

1

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];
}];
Sign up to request clarification or add additional context in comments.

10 Comments

what's the second rule of autolayout?
The second rule of AutoLayout. You can NOT CHANGE the frame or centre of views using AutoLayout.
@jrturton seriously though. There are like Five rules for AutoLayout :D Hmm... I feel a blog post coming.
@DuncanC AutoLayout is fantastic - unfortunately though I think Apple has just done a bad job of educating people. AutoLayout is generally much nicer in code than IB, although as of Xcode 5 IB isn't bad at all. Don't be afraid of it - embrace it!
Or @jrturton's excellent series of Autolayout blog posts: commandshift.co.uk/blog/2013/01/13/autolayout
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.