代码之家  ›  专栏  ›  技术社区  ›  amir.nh

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

  •  0
  • amir.nh  · 技术社区  · 7 年前

    2 回复  |  直到 7 年前
        1
  •  1
  •   PGDev    7 年前

    button 单击事件,只需为 x coordinate of origin imageView

    例子:

    @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 按钮 imageView's 已移至 extreme right 屏幕的( UIScreen.main.bounds.width

    2. 什么时候 de-selecting , x原点坐标 已移至 extreme left 屏幕的( 0.0 )

        2
  •  0
  •   Abhishek Jain    7 年前

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