我試圖在按鈕被點擊時從底部帶入子視圖。但只是第一次按鈕是可點擊的。在動畫按鈕不可點擊後再次點擊。第一次點擊後,UIButton不可點擊
這是代碼。
class AnimateView: UIView {
var button: UIButton!
var menuView: UIView!
var mainView: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
mainView = UIView(frame: CGRect(x: 0, y: 0, width:
self.frame.size.width, height: self.frame.size.height))
mainView.clipsToBounds = true
mainView.backgroundColor = .clear
mainView.isUserInteractionEnabled = true
mainView.isExclusiveTouch = true
self.addSubview(mainView)
let theRect = CGRect(x: self.frame.size.width/2 - 44/2, y: 0,
width: 44, height: 44)
button = UIButton(frame: theRect)
check.layer.cornerRadius = btnView.frame.size.height/2
mainView.addSubview(self.check)
mainView.bringSubview(toFront: check)
check.addTarget(self, action: #selector(buttonClick(_:)),
for:.touchUpInside)
let newRect = CGRect(x: 0, y: check.frame.height, width:
self.frame.size.width, height: self.mainView.frame.size.height)
menuView = UIView(frame: newRect)
menuView.backgroundColor = .blue
mainView.addSubview(menuView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func buttonClick(_ sender: UIButton) {
toggleMenu()
}
func toggleMenu() {
if check.transform == CGAffineTransform.identity {
UIView.animate(withDuration: 1, animations: {
self.check.transform = CGAffineTransform(scaleX: 12, y: 12)
self.mainView.transform = CGAffineTransform(translationX: 0, y: -120)
self.check.transform = CGAffineTransform(rotationAngle: self.radians(180)) // 180 is 3.14 radian
self.setNeedsLayout()
}) { (true) in
UIView.animate(withDuration: 0.5, animations: {
})
}
} else {
UIView.animate(withDuration: 1, animations: {
// .identity sets to original position
//self.btnView.transform = .identity
self.mainView.transform = .identity
self.button.transform = .identity
})
}
}
}
這個類是從另一個UIView類這樣調用
class MainViewClass: UIView {
var animateView: AnimateView!
override init(frame: CGRect) {
animateView = AnimateView(frame: CGRect(x: 0, y: self.frame.size.height - 44 - 44 - 20, width: self.frame.size.width, height: 122+44))
//animateView.clipsToBounds = true
self.addSubview(animateView)
self.bringSubview(toFront: animateView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
當屏幕上首次出現:
現在該按鈕不可點擊。當我看到按鈕的框架時,它與屏幕最初出現時的相同,即使按鈕觸摸屏幕底部,即使它向上移動。 我想要的是當點擊按鈕時從底部顯示帶有動畫的屏幕並且第二次點擊移動到默認位置。
但是,如果我把按鈕作爲內部自我子視圖,而不是作爲子視圖的MAINVIEW,則該按鈕爲可點擊的,但鑑於是動畫和向上移動即使它仍然在相同的位置。
self.addSubview(按鈕)
你需要它完成動畫後刪除您'AnimateView',使按鍵再次暴露? –
首先當點擊按鈕時,子視圖應該從屏幕底部開始動畫,並且按鈕也應該向上移動。然後第二次點擊按鈕應該與子視圖一起下去。 現在,當點擊按鈕時,子視圖與按鈕按鈕一起向上動畫不可點擊。 – bthapa
單擊按鈕並向上移動時,該按鈕不可點擊。雖然我嘗試更新其框架,但它不可點擊。 – bthapa