7

How can I do a simple position changing for UIImageView. Lets say that current coordinates are x: 20 and y:30

I want to move it to x:100 and y:100.

Is it possible to do animation of movement?

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Bob
  • 8,392
  • 12
  • 55
  • 96
  • Possible duplicate of [Simple way to change the position of UIView?](https://stackoverflow.com/questions/5161096/simple-way-to-change-the-position-of-uiview) – Jon Schneider Mar 19 '19 at 03:15

2 Answers2

31

You need to change the CGFrame of that UIImageView Like so -

[imageView setFrame:CGRectMake(100, 100, imageView.frame.size.width, imageView.frame.size.height)];

This changes the uiimageview poistion to (100, 100) while keeping the height and width same. Note that you can use the above code to not only change the (x,y) position but also the size of the view. Using the below code you can animate it too :)

If you want to animate the position change -

[UIView animateWithDuration:0.35 
                      delay:0.0  
                    options: UIViewAnimationCurveEaseOut
                 animations:^{ 
                     [imageView setFrame:CGRectMake(100, 100, imageView.frame.size.width, imageView.frame.size.height)];
                 } 
                 completion:^(BOOL finished){
                     NSLog(@"Done!");
                 }];
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • I got this error when I try your code (for the animated version) Sending 'int' to parameter of incompatible type 'CGRect' (aka 'struct CGRect') for the line with CGFrameMake. Do you know maybe why? – Bob Nov 11 '12 at 17:13
  • sorry it is `CGRectMake` not `CGFrameMake`. Typo!!! Updated the code above too... Try it now again. – Srikar Appalaraju Nov 11 '12 at 17:18
  • this is perfect. you only missing one bracket. I will add it now... vote up for perfect answer! – Bob Nov 11 '12 at 17:22
  • 1
    thanks, glad to help! I typed out the code. I guess I have become too dependent on Xcode's syntax completion ;) – Srikar Appalaraju Nov 11 '12 at 17:30
  • @Srikar Appal how can i get coordinates of the image after the duration – Agent Chocks. Jul 17 '13 at 13:13
  • @AbhijitChaudhari after the duration? Huh @#! you are setting the frame values right? So after the animation it would be the same coordinates that you have set. Did i understand you correct ? – Srikar Appalaraju Jul 17 '13 at 13:17
  • @Srikar Appal yes but on simulator i see the image moving so i need to calculate the position after some span of time sry if my approach is incorrect please suggest me something – Agent Chocks. Jul 17 '13 at 13:32
  • @AbhijitChaudhari can you tell me exactly why you are trying to do? and please be as elaborate as possible. – Srikar Appalaraju Jul 17 '13 at 16:14
  • @Srikar Appal please see this http://stackoverflow.com/questions/17714332/get-coordinates-of-animated-uiimageview – Agent Chocks. Jul 18 '13 at 04:05
  • I had to change `UIViewAnimationCurveEaseOut` to `UIViewAnimationOptionCurveEaseOut` - but otherwise it works brilliantly. – mark.ed Jun 04 '15 at 14:11
  • 1
    If @SrikarAppalaraju - or anyone else is still watching: I want the image to move with a button press. And it does - but it's leaving the 'old' image. Added a [self.myImage setNeedsDisplay]; - but no luck. Any suggestions? I'm only using this because I 1st created 10 separate image views at the proper location and was using HIDDEN= NO and YES to turn them on and off. Worked for a storyboard created image view but not for a programmatic obj-c one. – user3741598 Jan 10 '21 at 21:18
1
self.psView.frame = CGRectMake(self.wvView.frame.origin.x, self.wvView.frame.origin.y, self.psView.frame.size.width, self.psView.frame.size.height);
Blue
  • 22,608
  • 7
  • 62
  • 92
  • 3
    Welcome to Stack Overflow! While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Aug 25 '16 at 05:55