【问题标题】:hide/show uiimageview with animation when clicking button单击按钮时隐藏/显示带有动画的uiimageview
【发布时间】:2018-01-22 08:06:42
【问题描述】:

如何实现,当我单击按钮时,我的图像会以动画形式移出屏幕,当我再次单击按钮时,又会回到原来的位置?

【问题讨论】:

  • 请显示一些代码,你尝试了什么?
  • 将图像添加到 uiview 并使用块为 uiview 设置动画。您可以在应用中播放您想要的动画类型

标签: ios iphone swift animation swift2


【解决方案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. selectingbutton,imageView'sx coordinate of origin移动到屏幕的extreme right时(UIScreen.main.bounds.width)

  2. de-selectingbutton,imageView'sx coordinate of origin移动到屏幕的extreme left时(0.0)

【讨论】:

    【解决方案2】:

    使用此代码向左移动-

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

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2018-01-10
      • 2016-05-21
      • 2023-01-10
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      相关资源
      最近更新 更多