【发布时间】:2019-04-06 15:57:57
【问题描述】:
我正在尝试添加从底部显示的横幅:
所以我创建了一个UIView,里面有一个UIButton。问题是当我点击按钮时,什么也没有发生。
@objc func myButtonAction() {
print("My Button tapped")
}
func notifBanner(message: String, color: UIColor, backgroundColor: UIColor, button: UIButton){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let window = appDelegate.window!
if infoViewIsShowing == false{
infoViewIsShowing = true
let grey1 = UIColor(red: 196/255, green: 196/255, blue: 196/255, alpha: 1)
let infoViewHeight = CGFloat(50)
let infoViewY = window.bounds.height + infoViewHeight
let infoView = UIView(frame: CGRect(x: 10, y: infoViewY, width: window.bounds.width - 20, height: infoViewHeight))
infoView.backgroundColor = backgroundColor
infoView.layer.cornerRadius = 8
infoView.clipsToBounds = true
infoView.isUserInteractionEnabled = true
window.addSubview(infoView)
infoView.alpha = 0.4
let widthButton = CGFloat(115)
let goToProfileButton = UIButton()
goToProfileButton.frame = CGRect(x: 0, y: 0, width: infoLabelWidth, height: infoLabelHeight)
goToProfileButton.setTitle("View Profile", for: .normal)
goToProfileButton.tintColor = .white
goToProfileButton.addTarget(self, action: #selector(self.myButtonAction), for: .touchUpInside)
goToProfileButton.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
infoView.addSubview(goToProfileButton)
// Animate bannerView
UIView.animate(withDuration: 0.4, animations: {
infoView.alpha = 1
infoView.frame.origin.y = window.bounds.height - infoViewHeight - 60
}, completion: { (finished: Bool) in
if finished{
UIView.animate(withDuration: 0.4, delay: 3, options: .curveEaseIn, animations: {
// move up
infoView.frame.origin.y = infoViewY
infoView.alpha = 0.3
}, completion: { (finished: Bool) in
if finished {
infoView.removeFromSuperview()
self.infoViewIsShowing = false
}
})
}
})
}
}
我尝试添加 infoView.isUserInteractionEnabled = true 但它仍然无法正常工作。当我点击按钮时,您知道如何发送操作吗?
【问题讨论】:
-
如果你做了
infoView.isUserInteractionEnabled = false呢? -
@Sweeper 仍然无法正常工作
-
在 Xcode 中调试视图层次结构,确保按钮在其父视图的框架内,并确保按钮没有被任何东西覆盖。
-
问题出在 animate() 中,如果没有动画,则无法正常工作。但是不知道怎么保留动画,保留addTarget函数