2017-08-14 14 views
0

ボタンをクリックすると、イメージがアニメーション付きで画面外に移動し、ボタンを再度クリックするとその場所に戻ります。ボタンをクリックすると、アニメーションのuiimageviewが表示されます

+0

に移動したとき、あなたは何を試してみましたか? – 3stud1ant3

+0

ブロックを使用して、画像にuiviewとanimate uiviewを追加します。あなたはあなたのアプリでどんな種類のアニメーションで遊ぶことができます – cole

答えて

1

右に移動する左

func moveToLeft() 
{ 
    var frame = imageView.frame 
    frame.origin.x = -imageView.frame.size.width //Adjust this value according to your need 
    UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseOut, animations: {() -> Void in 
     self.imageView.frame = frame 
    }, completion: {(_ finished: Bool) -> Void in 
     /*done*/ 
    }) 
} 

使用このコードを移動します。

例:

@IBAction func onTapButton(_ sender: UIButton) 
{ 
    if sender.isSelected 
    { 
     UIView.animate(withDuration: 2.0, animations: { 
      self.imageView.frame.origin.x = 0.0 
     }) 
    } 
    else 
    { 
     UIView.animate(withDuration: 2.0, animations: { 
      self.imageView.frame.origin.x = UIScreen.main.bounds.width 
     }) 
    } 
    sender.isSelected = !sender.isSelected 
} 

次のように上記のコードは動作します:

  1. selectingbuttonimageView'sx coordinate of originが画面のextreme rightUIScreen.main.bounds.width

  2. に移動させます

    de-selectingbuttonimageView'sx coordinate of originは、いくつかのコードを表示してください画面のextreme left0.0

0

使用このコードは、単にあなたの条件に応じてimageViewx coordinate of originをアニメーション、buttonクリックイベントで

func moveToRight() 
{ 
    var frame = imageView.frame 
    frame.origin.x = 0 //Adjust this value according to your need 
    UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseOut, animations: {() -> Void in 
     self.imageView.frame = frame 
    }, completion: {(_ finished: Bool) -> Void in 
     /*done*/ 
    }) 
} 
関連する問題