2013-02-27 46 views
0

我有一個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://stackoverflow.com/questions/6145337/how-to-create-a-moving-image – Kyle 2013-02-27 19:11:04

回答

4

嘗試使用動畫

[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