当点击按钮时,我怎样才能实现当我点击按钮时,我的图像用动画移出屏幕,当我再次点击按钮时,它会回到原来的位置,隐藏/显示带动画的uiimageview点击按钮

问题描述:

当点击按钮时,我怎样才能实现当我点击按钮时,我的图像用动画移出屏幕,当我再次点击按钮时,它会回到原来的位置,隐藏/显示带动画的uiimageview点击按钮

+0

请出示一些代码的extreme left,你尝试过什么? – 3stud1ant3

+0

使用块将图像添加到uiview并使uiview动画。你可以玩你想要的应用程序中的动画 – cole

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

使用此代码移动左手

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