我想在按下uiview时更改其边框颜色,并在释放后返回到正常的边框颜色。最佳做法是什么?
如下图所示:
最佳答案
不要使用 UITapGestureRecognizer 。您可以使用 UILongPressGestureRecognizer
代码:
override func viewDidLoad() {
super.viewDidLoad()
let view = UIView.init(frame: CGRect.init(x: 30, y: 200, width: 100, height: 40))
self.view.addSubview(view)
view.layer.borderColor = UIColor.black.cgColor
view.layer.borderWidth = 3
let tapForView = UILongPressGestureRecognizer(target: self, action: #selector(self.toChangeColor(recognizer:)))
tapForView.minimumPressDuration = 0.01
view.isUserInteractionEnabled = true
view.addGestureRecognizer(tapForView)
}
@objc func toChangeColor(recognizer:UILongPressGestureRecognizer)
{
// Apply logic for changing background color.
let view = recognizer.view
if recognizer.state == .began {
view?.layer.borderColor = UIColor.orange.cgColor
print("view began")
}
else if recognizer.state == .ended {
view?.layer.borderColor = UIColor.black.cgColor
print("view ended")
}
}
这将完美地工作。
关于ios - 如何更改UIview的边框颜色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47789444/