Adam Carter
Adam Carter

Reputation: 4844

Objective C - Make a UIImageView jump

I have been stuck trying to make my character jump, and have tried both hardcoding the y position of the character and using uiimageview animations. What is the best way to do it and the best example of code (because I think I have done the two previous examples wrong) - as I am quite new.

Upvotes: 0

Views: 781

Answers (1)

Michał Zygar
Michał Zygar

Reputation: 4092

Here is sample code using blocks(triggered on button action).

- (IBAction)btnClicked:(id)sender {
    [UIView animateWithDuration:0.5f animations:^{
        CGRect currentRect=self.imgView.frame;
        currentRect.origin.y-=30;

        [self.imgView setFrame:currentRect];

    } completion:^(BOOL finished) {

       [UIView animateWithDuration:0.5f animations:^{
          CGRect currentRect=self.imgView.frame;

          currentRect.origin.y+=30;

          [self.imgView setFrame:currentRect];

      }];
    }];
    }

Upvotes: 2

Related Questions