UIImageView弹出和返回

问题描述:

我有一个UIImage,当我点击它时,我希望它在X方向上移动2px,在Y方向上移动2px,然后移回原始位置。这样它可以被多次按下,只是轻摇。UIImageView弹出和返回

我到目前为止是这样的;

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

这使得图像每个方向移动2px。

我试过了;

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

但这只会使图像无法移动。这些行在viewDidLoad中。

我需要的是让它在被点击到原始位置后立即返回。

+0

我猜你的第二个代码只是被如此之快,它没有显示在屏幕上时执行。你有没有考虑过使用其中一个面向动画的类来执行这个'微动'?看看这个问题,它显示了如何使用动画,不同的动作,但你可以调整坐标。 http://*.com/questions/6145337/how-to-create-a-moving-image – Kyle 2013-02-27 19:11:04

尝试使用动画

[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); 
       }]; 
     } 
    }]; 
+0

完美的工作,非常感谢! – GMA 2013-02-27 19:20:23

+0

如果有效,请确保提高投票率并标记为正确答案。那些回复自己花时间帮助别人的人。 – 2013-02-27 19:24:22