2016-03-01 63 views
1

當我點擊圖片時需要將UIButtons移動到視圖中,並且當我再次點擊同一圖片時需要將UIButton移出屏幕。當我點擊圖片時移動UIButtons

當我點擊一個UIImage時,4個UIButtons在屏幕內移動,下面的代碼沒有問題。

func imageTapped(img: AnyObject) 
     {UIView.animateWithDuration(0.5, 
     delay: 0, 
     options: [.CurveEaseInOut, .AllowUserInteraction], 
     animations: { 
      self.imageReceivedTrashCan.center = CGPoint(x: 30, y: 530) 
      self.imageReceivedRightArrow.center = CGPoint(x: 285, y: 530) 
      self.imageReceivedForward.center = CGPoint(x: 160, y: 530) 
      self.imageReceivedCross.center = CGPoint(x: 30, y: 30) 
     }, 
     completion: { finished in 
      print("Objects moved!") 
    }) 
} 

但是,我無法找到一種方法,我怎麼可以移動UIButtons回當我再次同一幅圖像上單擊原來的位置。感謝你的幫助。

+0

您似乎總是按鈕(圖像)移動到?圖片。你需要檢查垃圾桶,箭頭等是否在圖像內部,如果是的話,將它們移出。放置一個條件代碼塊,將圖像移回。 – wottle

回答

3

即使這似乎真的老派,W/O任何自動佈局和我隨機猜你不改變該中心的其他地方,所有中心的一次:

func imageTapped(img: AnyObject) { 
    if (self.imageReceivedTrashCan.center == CGPoint(x: 30, y: 530)) { 
     UIView.animateWithDuration(0.5, 
     delay: 0, 
     options: [.CurveEaseInOut, .AllowUserInteraction], 
     animations: { 
      self.imageReceivedTrashCan.center = CGPoint(x: -30, y: -530) 
      self.imageReceivedRightArrow.center = CGPoint(x: -285, y: -530) 
      self.imageReceivedForward.center = CGPoint(x: -160, y: -530) 
      self.imageReceivedCross.center = CGPoint(x: -30, y: -30) 
     }, 
     completion: { finished in 
      print("Objects moved!") 
    }) 
} else { 
UIView.animateWithDuration(0.5, 
     delay: 0, 
     options: [.CurveEaseInOut, .AllowUserInteraction], 
     animations: { 
      self.imageReceivedTrashCan.center = CGPoint(x: 30, y: 530) 
      self.imageReceivedRightArrow.center = CGPoint(x: 285, y: 530) 
      self.imageReceivedForward.center = CGPoint(x: 160, y: 530) 
      self.imageReceivedCross.center = CGPoint(x: 30, y: 30) 
     }, 
     completion: { finished in 
      print("Objects moved!") 
    }) 
} 
} 
+0

非常感謝。 – OzzY

0
  • 首先,要回答你問題:

您可以保存爲每個按鈕的初始位置,然後在第二次敲擊你打電話animateWithDuration:delay:options:animations:completion:和位置設置爲self.imageReceivedTrashCan.center = initialPosition

您也可以嘗試使用CoreGraphics的翻譯方法。

UIView.animateWithDuration(0.5, 
     delay: 0, 
     options: [.CurveEaseInOut, .AllowUserInteraction], 
     animations: { 
       self.imageReceivedTrashCan.transform = CGAffineTransformMakeTranslation(300, 0) 
       .... 
}, 
     completion: { finished in 
      print("Objects moved!") 
    }) 
} 

然後在第二次敲擊回到身份變換

UIView.animateWithDuration(0.5, 
     delay: 0, 
     options: [.CurveEaseInOut, .AllowUserInteraction], 
     animations: { 
       self.imageReceivedTrashCan.transform = CGAffineTransformIdentity 
       .... 
}, 
     completion: { finished in 
      print("Objects moved!") 
    }) 
} 
  • 但是這種方法是硬編碼的,你應該使用的autoLayout做出這種動畫。 Look at here

希望這有助於你;)

0


選擇的ImageView,轉到屬性檢查器和檢查用戶交互啓用

UITapGestureRecognizer *TapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moveMyView:)]; 
[imageview addGestureRecognizer: TapRecognizer]; 

- (void) moveMyView:(UITapGestureRecognizer*)sender { 

[UIView animateWithDuration:1.0 delay:0.0 options: 
UIViewAnimationOptionCurveEaseIn animations:^{ 
     self.imageReceivedTrashCan.center = CGPoint(x: -30, y: -530) 
     self.imageReceivedRightArrow.center = CGPoint(x: -285, y: -530) 
     self.imageReceivedForward.center = CGPoint(x: -160, y: -530) 
     self.imageReceivedCross.center = CGPoint(x: -30, y: -30) 
    } completion:^ (BOOL completed) {}]; 
}]; 

}