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

时间:2017-08-14 13:09:52

标签: ios iphone swift animation swift2

当我点击按钮时,我的图像是如何实现的,我的图像会随着动画移出屏幕,当我再次点击按钮时,会回到原位?

2 个答案:

答案 0 :(得分:1)

button点击事件中,只需根据您的要求为x coordinate of origin的{​​{1}}设置动画即可。

示例:

imageView

以上代码的工作原理如下:

  1. @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 } selectingbutton imageView's移至x coordinate of origin的{​​{1}}时

  2. extreme right UIScreen.main.bounds.widthde-selecting button移至imageView's的{​​{1}}时

答案 1 :(得分: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*/
    })
}