我试图在按钮被点击时从底部带入子视图。但只是第一次按钮是可点击的。在动画按钮不可点击后再次点击。第一次点击后,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