2017-08-14 77 views

回答

1

button點擊事件,只需根據您的要求動畫imageViewx coordinate of origin

實施例:

@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被移動到屏幕(UIScreen.main.bounds.width

  2. extreme right

    de-selectingbuttonimageView'sx coordinate of origin移動到屏幕(0.0

0

使用此代碼移動左手

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*/ 
    }) 
} 

使用此代碼移動右

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*/ 
    }) 
} 
相關問題