Search code examples
iphonecore-animation

moving uiimage view on screen


I want an image view of some size say 25 by 25, to move in a linear motion from left to right and then right left continuously.

Lets say the image starts moving from left, then at the time it reaches reaches the right, it should start moving back to the left, then this should repeat.

I have this code, which lets the view move down, but I am unable to move it back and forth. This is for UIView.

[UIView beginAnimations:@"MyAnimation" context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:45.0]; // 5 seconds
frame = movingView.frame;
//int j = frame.origin.y;
int i;
for (i= 0; i<=100; i++) {
    frame.origin.y += i;//100.0; // Move view down 100 pixels
    movingView.frame = frame;
}   

How should i do this?

Regards


Solution

  • You don't have to animate things yourself when using UIView animations.

    frame = movingView.frame;
    frame.origin.x = 0;
    movingView.frame = frame; // starting point
    
    [UIView beginAnimations:@"MyAnimation" context:nil];
    [UIView setAnimationDuration:5];
    
    frame.origin.x = 320-25;
    movingView.frame = frame; // endpoint
    
    [UIView commitAnimations];
    

    for a move from left to right. To wait for the end, use setAnimationDidStopSelector to call a method which does the opposite animation.

    Have a look at flipping uiview continuously for the animation delegate stuff.