单击 UIBarButton 显示 toast 和双击返回动作需要执行
on single tap UIBarButton display toast and on double tap back action need to perform
我在导航栏中有一个 UIBarButton,当我点击后退按钮(第一次点击)时我需要显示吐司(如警告),双击我需要退出 swift 中的页面,
以下用于显示吐司的代码,它工作正常,
let toastLabel = UILabel(frame: CGRect(x: 20, y: self.view.frame.size.height-100, width: 350, height: 35))
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = .center;
toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
toastLabel.text = "demo"
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
self.view.addSubview(toastLabel)
UIView.animate(withDuration: 2.0, delay: 0.1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
请指导我完成这个任务。
使用UIControlEventTouchDownRepeat事件,当tapCount
为2
时执行动作。
在UIButton上为控件事件UIControlEventTouchDownRepeat添加一个target-action,只在touch的tapCount为2时才做动作。如下Swift 3
button.addTarget(self, action: #selector(multipleTap(_:event:)), for: UIControlEvents.touchDownRepeat)
然后添加选择器如下
func multipleTap(_ sender: UIButton, event: UIEvent) {
let touch: UITouch = event.allTouches!.first!
if (touch.tapCount == 2) {
// do action.
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
let button = UIButton(type: .system)
button.frame = CGRect(x: 0, y: 0, width: 80, height: 40)
button.setTitle("Back", for: .normal)
//Gesture Recognizer
let singleTap = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap))
singleTap.numberOfTapsRequired = 1
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
doubleTap.numberOfTapsRequired = 2
button.addGestureRecognizer(singleTap)
button.addGestureRecognizer(doubleTap)
singleTap.require(toFail: doubleTap)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.leftBarButtonItem = barButton
}
@objc func handleSingleTap(sender: UITapGestureRecognizer? = nil) {
let toastLabel = UILabel(frame: CGRect(x: 20, y: self.view.frame.size.height-100, width: 350, height: 35))
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = .center;
toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
toastLabel.text = "demo"
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
self.view.addSubview(toastLabel)
UIView.animate(withDuration: 2.0, delay: 0.1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
print("Single Tap detected")
}
@objc func handleDoubleTap(sender: UITapGestureRecognizer? = nil) {
print("Double Tap detected")
}
我在导航栏中有一个 UIBarButton,当我点击后退按钮(第一次点击)时我需要显示吐司(如警告),双击我需要退出 swift 中的页面,
以下用于显示吐司的代码,它工作正常,
let toastLabel = UILabel(frame: CGRect(x: 20, y: self.view.frame.size.height-100, width: 350, height: 35))
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = .center;
toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
toastLabel.text = "demo"
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
self.view.addSubview(toastLabel)
UIView.animate(withDuration: 2.0, delay: 0.1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
请指导我完成这个任务。
使用UIControlEventTouchDownRepeat事件,当tapCount
为2
时执行动作。
在UIButton上为控件事件UIControlEventTouchDownRepeat添加一个target-action,只在touch的tapCount为2时才做动作。如下Swift 3
button.addTarget(self, action: #selector(multipleTap(_:event:)), for: UIControlEvents.touchDownRepeat)
然后添加选择器如下
func multipleTap(_ sender: UIButton, event: UIEvent) {
let touch: UITouch = event.allTouches!.first!
if (touch.tapCount == 2) {
// do action.
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
let button = UIButton(type: .system)
button.frame = CGRect(x: 0, y: 0, width: 80, height: 40)
button.setTitle("Back", for: .normal)
//Gesture Recognizer
let singleTap = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap))
singleTap.numberOfTapsRequired = 1
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
doubleTap.numberOfTapsRequired = 2
button.addGestureRecognizer(singleTap)
button.addGestureRecognizer(doubleTap)
singleTap.require(toFail: doubleTap)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.leftBarButtonItem = barButton
}
@objc func handleSingleTap(sender: UITapGestureRecognizer? = nil) {
let toastLabel = UILabel(frame: CGRect(x: 20, y: self.view.frame.size.height-100, width: 350, height: 35))
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = .center;
toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
toastLabel.text = "demo"
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
self.view.addSubview(toastLabel)
UIView.animate(withDuration: 2.0, delay: 0.1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
print("Single Tap detected")
}
@objc func handleDoubleTap(sender: UITapGestureRecognizer? = nil) {
print("Double Tap detected")
}