触摸开始时使多个按钮突出显示
Make multiple buttons highlight when touch begins
我将三个按钮放在一个视图中,每个按钮都有不同的个人资料图片。当开始触摸任何一个按钮时,我需要所有三个按钮都突出显示,以便三个按钮一起看起来是一个按钮。我尝试了以下代码,但它不起作用。有什么想法吗?
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap))
tapGesture.numberOfTapsRequired = 1
friendsBtn1.addGestureRecognizer(tapGesture)
func normalTap(sender: UITapGestureRecognizer){
if sender.state == .began {
friendsBtn2.isHighlighted = true
friendsBtn3.isHighlighted = true
}
if sender.state == .ended {
friendsBtn2.isHighlighted = false
friendsBtn3.isHighlighted = false
}
print("Normal tap")
}
为按钮操作中的所有三个按钮尝试 [button setSelected:YES];
。
我觉得没必要UITapGesture
你可以这样管理。
首先,您需要将突出显示的图像设置为按钮和设置按钮事件,如下所示。
override func viewDidLoad() {
super.viewDidLoad()
friendsBtn2.setImage(UIImage(named: "highlighted.png"), for: .highlighted)
friendsBtn3.setImage(UIImage(named: "highlighted.png"), for: .highlighted)
friendsBtn1.addTarget(self, action: #selector(YourViewController.touchDownEvent), for: .touchDown)
friendsBtn1.addTarget(self, action: #selector(YourViewController.touchUpInsideEvent), for: .touchUpInside)
}
之后在你的 ViewController.
添加方法 touchDownEvent()
,touchUpInsideEvent()
func touchDownEvent() {
self.allButtonHighlighted(fleg: true)
}
func touchUpInsideEvent() {
self.allButtonHighlighted(fleg: false)
}
func allButtonHighlighted( fleg: Bool) {
friendsBtn2.isHighlighted = fleg
friendsBtn3.isHighlighted = fleg
}
我希望它会起作用。
我确实解决了问题。这是代码。
[friendsBtn1, friendsBtn2, friendsBtn3].forEach { button in
button?.addTarget(self, action:#selector(highlightAllButtons(sender:)), for: .touchDown)
button?.addTarget(self, action:#selector(unhighlightAllButtons(sender:)), for: [.touchUpInside, .touchUpOutside])
}
func highlightAllButtons(sender: UIButton) {
[friendsBtn1, friendsBtn2, friendsBtn3].forEach { button in
button.isHighlighted = true
}
}
func unhighlightAllButtons(sender: UIButton) {
[friendsBtn1, friendsBtn2, friendsBtn3].forEach { button in
button.isHighlighted = false
}
}
我将三个按钮放在一个视图中,每个按钮都有不同的个人资料图片。当开始触摸任何一个按钮时,我需要所有三个按钮都突出显示,以便三个按钮一起看起来是一个按钮。我尝试了以下代码,但它不起作用。有什么想法吗?
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap))
tapGesture.numberOfTapsRequired = 1
friendsBtn1.addGestureRecognizer(tapGesture)
func normalTap(sender: UITapGestureRecognizer){
if sender.state == .began {
friendsBtn2.isHighlighted = true
friendsBtn3.isHighlighted = true
}
if sender.state == .ended {
friendsBtn2.isHighlighted = false
friendsBtn3.isHighlighted = false
}
print("Normal tap")
}
为按钮操作中的所有三个按钮尝试 [button setSelected:YES];
。
我觉得没必要UITapGesture
你可以这样管理。
首先,您需要将突出显示的图像设置为按钮和设置按钮事件,如下所示。
override func viewDidLoad() {
super.viewDidLoad()
friendsBtn2.setImage(UIImage(named: "highlighted.png"), for: .highlighted)
friendsBtn3.setImage(UIImage(named: "highlighted.png"), for: .highlighted)
friendsBtn1.addTarget(self, action: #selector(YourViewController.touchDownEvent), for: .touchDown)
friendsBtn1.addTarget(self, action: #selector(YourViewController.touchUpInsideEvent), for: .touchUpInside)
}
之后在你的 ViewController.
添加方法touchDownEvent()
,touchUpInsideEvent()
func touchDownEvent() {
self.allButtonHighlighted(fleg: true)
}
func touchUpInsideEvent() {
self.allButtonHighlighted(fleg: false)
}
func allButtonHighlighted( fleg: Bool) {
friendsBtn2.isHighlighted = fleg
friendsBtn3.isHighlighted = fleg
}
我希望它会起作用。
我确实解决了问题。这是代码。
[friendsBtn1, friendsBtn2, friendsBtn3].forEach { button in
button?.addTarget(self, action:#selector(highlightAllButtons(sender:)), for: .touchDown)
button?.addTarget(self, action:#selector(unhighlightAllButtons(sender:)), for: [.touchUpInside, .touchUpOutside])
}
func highlightAllButtons(sender: UIButton) {
[friendsBtn1, friendsBtn2, friendsBtn3].forEach { button in
button.isHighlighted = true
}
}
func unhighlightAllButtons(sender: UIButton) {
[friendsBtn1, friendsBtn2, friendsBtn3].forEach { button in
button.isHighlighted = false
}
}