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?

vadian
  • 274,689
  • 30
  • 353
  • 361
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
  • possible duplicate of [How do I animate constraint changes?](http://stackoverflow.com/questions/12622424/how-do-i-animate-constraint-changes) – Scott Berrevoets Jan 27 '14 at 15:53

1 Answers1

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];
}];
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • what's the second rule of autolayout? – jrturton Jan 27 '14 at 16:18
  • The second rule of AutoLayout. You can NOT CHANGE the frame or centre of views using AutoLayout. – Fogmeister Jan 27 '14 at 16:20
  • @jrturton seriously though. There are like Five rules for AutoLayout :D Hmm... I feel a blog post coming. – Fogmeister Jan 27 '14 at 16:23
  • The second rule of AutoLayout is abandon hope, all ye who enter. :/ The tools are half-baked and ill-conceived, and you should expect to waste many hours trying to figure out how to make it work. – Duncan C Jan 27 '14 at 18:45
  • Just to add, `self.view layoutIfNeeded` does take a performance hit as compared to shifting of frames!! – footyapps27 Jan 28 '14 at 03:56
  • 1
    @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! – James Frost Jan 28 '14 at 10:50
  • @JamesFrost agree completely. @ DuncanC if you're having trouble with it I found the Ray Wenderlich books were an absolute god send. http://www.raywenderlich.com/store/ios-7-and-ios-games-by-tutorials-bundle – Fogmeister Jan 28 '14 at 10:58
  • 1
    Or @jrturton's excellent series of Autolayout blog posts: http://commandshift.co.uk/blog/2013/01/13/autolayout/ – James Frost Jan 28 '14 at 10:59
  • @JamesFrost that's what I meant... :D – Fogmeister Jan 28 '14 at 11:00
  • @JamesFrost, AutoLayout is reasonable from code, and has some powerful features. What I don't like (despise, actually) is the IB tools to edit forms using AuotLayout. What used to be trivial using struts and springs now becomes 3X as many steps, and the appearance of a form in IB often has nothing at all to do with the way it will look on-screen. The tools for viewing and editing constraints are ham-handed and awkward. – Duncan C Jan 28 '14 at 17:48