我正在尝试添加一个从底部出现的横幅:

所以我创建了一个内部带有UIButton的UIView。问题是,当我点击按钮时,什么也没有发生。
@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,但它仍然不起作用。你知道当我点击按钮时如何发送动作吗?
发布于 2019-04-07 00:47:49
假设您希望能够在按钮出现的3秒内轻击按钮,您可以尝试将代码更改为使用DispatchQueue asyncAfter。
UIView.animate(withDuration: 0.4, animations: {
infoView.alpha = 1
infoView.frame.origin.y = window.bounds.height - infoViewHeight - 60
}, completion: { (finished: Bool) in
if finished{
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
UIView.animate(withDuration: 0.4, delay: 0, 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
}
})
}
}
})发布于 2019-04-07 00:02:58
您需要添加UITapGestureRecognizer,它将为您工作
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
myView.addGestureRecognizer(tap)
@objc func handleTap(sender: UITapGestureRecognizer? = nil) {
// handling code
}https://stackoverflow.com/questions/55550849
复制相似问题