点击更改视图颜色然后返回原始颜色
change view colour on tap then back to original colour
我正在尝试使用 UITapGestureRecognizer 在点击视图时更改 UIView 颜色,然后在 space 在 UIView 外部点击时恢复为原始白色。我可以让 UIView 改变颜色,但我无法让它变回白色。
// viewDidLoad
let tapGestureRegonizer = UITapGestureRecognizer(target: self, action:
#selector(mortgagePenaltyVC.viewCellTapped(recognizer:)))
tapGestureRegonizer.numberOfTapsRequired = 1
mortgageLenerViewCell.addGestureRecognizer(tapGestureRegonizer)
@objc func viewCellTapped (recognizer: UITapGestureRecognizer){
print("label Tapped")
mortgageLenerViewCell.backgroundColor = UIColor.lightGray
}
我认为您可以使用 touchesBegan
方法在视图之外进行触摸,如下所示
这段代码对我有用...
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
guard let location = touch?.location(in: mortgageLenerViewCell) else { return }
if !labelToClick.frame.contains(location) {
print("Tapped outside the view")
mortgageLenerViewCell.backgroundColor = UIColor.white
}else {
print("Tapped inside the view")
mortgageLenerViewCell.backgroundColor = UIColor.lightGray
}
}
这里labelToClick
是你要点击的标签&mortgageLenerViewCell
是labelToClick
的父视图
如有任何疑问,请随时提出...
谢谢。
我正在尝试使用 UITapGestureRecognizer 在点击视图时更改 UIView 颜色,然后在 space 在 UIView 外部点击时恢复为原始白色。我可以让 UIView 改变颜色,但我无法让它变回白色。
// viewDidLoad
let tapGestureRegonizer = UITapGestureRecognizer(target: self, action:
#selector(mortgagePenaltyVC.viewCellTapped(recognizer:)))
tapGestureRegonizer.numberOfTapsRequired = 1
mortgageLenerViewCell.addGestureRecognizer(tapGestureRegonizer)
@objc func viewCellTapped (recognizer: UITapGestureRecognizer){
print("label Tapped")
mortgageLenerViewCell.backgroundColor = UIColor.lightGray
}
我认为您可以使用 touchesBegan
方法在视图之外进行触摸,如下所示
这段代码对我有用...
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
guard let location = touch?.location(in: mortgageLenerViewCell) else { return }
if !labelToClick.frame.contains(location) {
print("Tapped outside the view")
mortgageLenerViewCell.backgroundColor = UIColor.white
}else {
print("Tapped inside the view")
mortgageLenerViewCell.backgroundColor = UIColor.lightGray
}
}
这里labelToClick
是你要点击的标签&mortgageLenerViewCell
是labelToClick
的父视图
如有任何疑问,请随时提出...
谢谢。