UIbutton动画期间第一次单击不起作用

时间:2019-11-11 06:01:38

标签: swift animation uibutton

我正在尝试使用“关闭”按钮关闭UIView。触摸屏幕后,“关闭”按钮随动画一起出现,再次触摸屏幕时,该按钮随动画消失。我编码为单击该按钮后,将关闭当前的UIView。但是,一旦按钮第一次出现,它不会使UIView消失,但是从第二次单击起,它就可以正常工作。下面是代码。请帮帮我!

class TestViewController: UIViewController {

    var btnToCloseMediaViewer:UIButton = UIButton()
    var btnToCloseMediaViewerAlpha:Bool = false

    var btnToCloseMediaViewerForTitlePage:UIButton = UIButton()
    var btnToCloseMediaViewerForTitlePageAlpha:Bool = false

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if btnToCloseMediaViewerForTitlePageAlpha == false {
            btnToCloseMediaViewerForTitlePageAlpha = true
            closeButtonForTitlePage (calculatedContraintforCloseButton: adjustedLeadingConstraintForButton!)
        } else {
            btnToCloseMediaViewerForTitlePageAlpha = false
            closeButtonForTitlePageRemoved (calculatedContraintforCloseButton: adjustedLeadingConstraintForButton!)
        }
    }

    func closeButtonForTitlePage (calculatedContraintforCloseButton: CGFloat) {
        self.btnToCloseMediaViewerForTitlePage.translatesAutoresizingMaskIntoConstraints = false
        self.btnToCloseMediaViewerForTitlePage.setTitle(“Close”, for: .normal)
        self.btnToCloseMediaViewerForTitlePage.setTitleColor(UIColor.white, for: .normal)
        UIView.animate(
            withDuration: 1.0,
            delay: 0.0,
            options: [UIView.AnimationOptions.curveEaseOut, .allowUserInteraction], animations: {
                self.btnToCloseMediaViewerForTitlePage.alpha = 1
                self.view.addSubview(self.btnToCloseMediaViewerForTitlePage)
        }, completion: {
            (value: Bool) in
            UIView.animate(
                withDuration: 1.5,
                delay: 1.5,
                options: [UIView.AnimationOptions.curveEaseOut, .allowUserInteraction], animations: {
                    self.btnToCloseMediaViewerForTitlePage.alpha = 0
            }, completion: {
                (value: Bool) in
                self.btnToCloseMediaViewerForTitlePage.removeFromSuperview()
                self.btnToCloseMediaViewerForTitlePageAlpha = false
            })
        })

        self.btnToCloseMediaViewerForTitlePage.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
        self.btnToCloseMediaViewerForTitlePage.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: CGFloat(calculatedContraintforCloseButton)).isActive = true
        self.btnToCloseMediaViewerForTitlePage.frame.size = CGSize(width: 30, height: 30)
        self.btnToCloseMediaViewerForTitlePage.addTarget(self, action: #selector(removal), for: .touchUpInside)
    }

    func closeButtonForTitlePageRemoved (calculatedContraintforCloseButton: CGFloat) {
        UIView.animate(
            withDuration: 1,
            delay: 0,
            options: [UIView.AnimationOptions.curveEaseOut, .allowUserInteraction], animations: {
                self.btnToCloseMediaViewerForTitlePage.alpha = 0
        }, completion: {
            (value: Bool) in
            self.btnToCloseMediaViewerForTitlePage.removeFromSuperview()
            self.btnToCloseMediaViewerForTitlePageAlpha = false
        })
    }

    @objc func removal(_ : UIButton) {
        //navigationController?.popViewController(animated: true)

        // Lokcing Rotation if iPhone
        if UIDevice.current.model != "iPad" {
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.shouldRotate = false
        }

        self.dismiss(animated: true, completion: nil)
    }
}

0 个答案:

没有答案