繁体   English   中英

单击按钮隐藏/显示带有动画的uiimageview

[英]hide/show uiimageview with animation when clicking button

当我单击按钮时,我的图像将随着动画移出屏幕,而当我再次单击该按钮时,该如何恢复呢?

button单击事件上,只需根据需要为imageView x 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. selecting buttonimageView's x coordinate of origin移动到屏幕的extreme rightUIScreen.main.bounds.width

  2. de-selecting buttonimageView's x coordinate of origin移动到屏幕的extreme left0.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*/
    })
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM