Я пытаюсь закрыть 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)
}
}