作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想让 UIView
在按下后突出显示,并在释放后返回正常颜色。这样做的最佳做法是什么?
最佳答案
UIView
的子类:
class CustomView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.blue
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
backgroundColor = UIColor.red
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
backgroundColor = UIColor.blue
}
}
Apple 关于覆盖 touchesBegan
和 touchesEnded
:
When creating your own subclasses, call super to forward any events that you do not handle yourself. If you override this method without calling super (a common use pattern), you must also override the other methods for handling touch events [i.e.
touchesEnded
,touchesMoved
,touchesCancelled
], even if your implementations do nothing.
进一步阅读:
关于ios - 如何让 UIView 在按下时改变颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47776551/
我是一名优秀的程序员,十分优秀!