문제

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.

도움이 되었습니까?

해결책

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);
              }];
         }
     }];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top