Move an uiimageview animated

11,826

Solution 1

Firstly, apple doc is your friend. All of the information I'm giving you here is derived from this. Apple also provides a LOT of sample code, and you should definitely take a look at it.

The way you can (easily) accomplish this is using UIView animations. Assuming that you have a UIImageView for your image, you can use the animateWithDuration:(NSTimeInterval)duration animations:... method.

For example:

[UIView animateWithDuration:10.0f animations:^{
    //Move the image view to 100, 100 over 10 seconds.
    imageView.frame = CGRectMake(100.0f, 100.0f, imageView.frame.size.width, imageView.frame.size.height);
}];

You could get fancier by adding more and more options to the animation, getting a completion block, etc. This is all achieved with variations of the 'animateWithDuration' method. There are tons of tutorials on UIView animations out there, and tons of documentation.

If you don't want to use blocks (the ^{ ...code...} bit above) you can run your animation like this:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:10.0f];
imageView.frame = ...
[UIView commitAnimations];

Solution 2

If I understand it you can do an animation of your own object, in this case try the beginAnimation

...
initial position

[UIView beginAnimation];
[UIView setAnimationDuration:dim/35.];
[UIView setAnimationCurve:[UIViewAnimationCurveLinear];

your animation

[UIView commitAnimations];
...

with this calculation dim/35 calculations by the second place to get your animation 35px / s

Share:
11,826
NielsKuil
Author by

NielsKuil

Updated on June 29, 2022

Comments

  • NielsKuil
    NielsKuil almost 2 years

    How could I make a UIImage move from the bottom of the screen, to the top of the screen in 20 seconds (about 35 pixels per seconds) I don't mean you should drag it by yourself, but it should automatically move to the top. Thank you