gpt4 book ai didi

ios - 如何让 UIView 在按下时改变颜色?

转载 作者:搜寻专家 更新时间:2023-10-31 22:10:26 25 4
gpt4 key购买 nike

我想让 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 关于覆盖 touchesBegantouchesEnded:

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.

进一步阅读:

https://developer.apple.com/documentation/uikit/uiview

Proper practice for subclassing UIView?

关于ios - 如何让 UIView 在按下时改变颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47776551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com