1

I have this code below who move my UIView to left:

- (void)viewDidLoad {
    [super viewDidLoad];

    [UIView beginAnimations:nil context:NULL];

        [UIView setAnimationDuration:4];

        int xOriginal = 91;

        CGRect rect = imagem.frame;

        int x = rect.origin.x;
        int y = rect.origin.y;
        int w = rect.size.width;
        int h = rect.size.height;

        if(x == xOriginal){

            imagem.frame = CGRectMake(x+100, y, w, h);
        }else{

            imagem.frame = CGRectMake(x-100, y, w, h);
        }

        [UIView commitAnimations];

}

My coordinate of my view is x = 91 (Center of the superView), When I start my app my UIView start left and go to center, instead of center and go to right, Why this is happening?

How to make my UIView start in center (x=91) and go to right (91+100), instead of left to center?

user3781174
  • 245
  • 5
  • 16
  • Do you use autolayout ? Did you put your animation in your controller -viewDidLoad method ? You also might prefer using one of the `+[UIView animateWith...]` functions – Crazyrems Dec 19 '14 at 10:56
  • When you are creating the view, you are probably setting its initial frame to be in the 'wrong' place. Please show the code where you set the initial frame of the view. – AMI289 Dec 19 '14 at 10:56
  • I update my post, this is all the code have in my app (my app is very simple in this case..) I put my code inside viewdidload method and my view is using auto layout. – user3781174 Dec 19 '14 at 10:57
  • In the beginning of `viewDidLoad` try programatically set the frame to the correct position, i.e. `self.frame = CGRectMake(91, WANTED_Y_POSITION, WANTED_WIDTH, WANTED_HEIGHT);` – AMI289 Dec 19 '14 at 11:02

4 Answers4

2
[UIImageView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveLinear  animations:^{
                imagem.frame=CGRectMake(imagem.frame.origin.x+100, imagem.frame.origin.y, imagem.frame.size.width, imagem.frame.size.height);

   } completion:^(BOOL finished) {
                //code for completion
                NSLog(@"Animation complete");
            }];
Monikanta
  • 307
  • 2
  • 8
1
Initially image.frame = CGRectMake(91,100,20,20);
So imageview starting point is 0

[UIView animateWithDuration:5.0
  animations:^{
    //Animation code goes here
   image.frame = CGRectMake(191,100,20,20); //Now imageview moves from 0 to 100
  } completion:^(BOOL finished) {
    //Code to run once the animation is completed goes here
}];
madhu
  • 961
  • 9
  • 21
  • Although the code is correct, it doesn't directly answer his question since he's asking about moving the view from origin x of 91 towards 191, and your example is moving it from initial x of 0 to 100. If you'll edit it it'll probably be excepted as answer mate. – AMI289 Dec 19 '14 at 11:06
1

Autolayout and frame animations are not good friends. Try to animate constraints instead of directly the frame.

You can create outlets of your constraints, and set the constant property of the constraint in your code to move your views.
You also call -[view layoutIfNeeded] to refresh your constraints while in an animation block.

Else you can remove all of your view constraints and animate its frame fearlessly.

Crazyrems
  • 2,551
  • 22
  • 40
  • Hey Man it works, I disable the AutoLayout and works fine, now the image start center and go to right, instead of left to center, thanks. – user3781174 Dec 19 '14 at 11:14
  • 2
    You can do animations with AutoLayout, but you have to use a different approach. Instead of animating the position of your view directly, you animate changes to the view's constraints. – Duncan C Dec 19 '14 at 12:13
  • @DuncanC You have some tutorial who teach to how animate with auto layout on? – user3781174 Dec 19 '14 at 18:33
  • I have only dabbled with animation using AutoLayout. I do have a project on github that shows how to use constraint animation to shift views up as the keyboard appears, but the code is only there as comments. Check out https://github.com/DuncanMC/RandomBlobs and look at the section in the markdown about shifting views to make room for the keyboard. – Duncan C Dec 19 '14 at 19:32
  • I just added another answer that explains how to do it. – Duncan C Dec 19 '14 at 19:36
1

As the other poster says, you can't animate a view's frame in XIBs/storyboards that use AutoLayout.

Instead you have to animate a constraint that's attached to the view.

What you do is to create a constraint (or multiple constraints) and connect it to an IBOutlet. Then, in your animation code, you calculate the changes to the constraint(s) and change the constant for the appropriate constraints.

For example, if you wanted to shift the vertical position of a view, set up a constraint that sets the vertical position of your view, and link it to an IBOutlet called viewConstraint. Then you'd use code like this:

[myView animateWithDuration: .25
  animations: ^
  {
    viewConstraint.constant -= shiftAmount;
    [self.view layoutIfNeeded];
  }
];
Duncan C
  • 128,072
  • 22
  • 173
  • 272