GMA
GMA

Reputation: 3

UIImageView bounce forth and back

I've got a UIImage that when I click it, I want it to move 2px in X direction and 2px in Y direction, and then move back to the original position. So that it can be pressed multiple times, and just jiggle.

What I've got so far is this;

imageViewTwo.center = CGPointMake(imageViewTwo.center.x +2, imageViewTwo.center.y +2);

This makes the image move 2px each direction.

I tried this;

imageViewTwo.center = CGPointMake(imageViewTwo.center.x +2, imageViewTwo.center.y +2);
imageViewTwo.center = CGPointMake(imageViewTwo.center.x -2, imageViewTwo.center.y -2);

but that only made the image not move at all. These lines are in the viewDidLoad.

What I need is to make it go back right after it's been clicked to the original position.

Upvotes: 0

Views: 1802

Answers (1)

Shubhank
Shubhank

Reputation: 21805

try using animation

    [UIView animateWithDuration:0.5
                     animations:^
     {
         //move right
         imageViewTwo.center = CGPointMake(imageViewTwo.center.x +2, imageViewTwo.center.y +2);
     }
                     completion:^(BOOL completed)
     {
         if (completed)
         {
             //completed move right..now move left
             [UIView animateWithDuration:0.5
                              animations:^
              {
                  imageViewTwo.center = CGPointMake(imageViewTwo.center.x -2, imageViewTwo.center.y -2);
              }];
         }
     }];

Upvotes: 5

Related Questions