悬停时如何删除 UIButton 的灰色外观
How to remove the greyed out look of a UIButton when hovering over
在我的 iPhone 应用程序中,我在 Interface Builder
中创建了一堆 UIButton
。每个按钮都有一个 GestureRecognizer
来识别硬压力(3D 触摸),当施加足够的力时,我会更改 UIButton 图像并进行一些计算。
一切正常,唯一的问题是每个按钮在被轻轻触摸(悬停)时都会变成灰色。
如何摆脱这种行为?
以下是我的部分代码:
class ViewController: UIViewController {
@IBOutlet weak var btn: UIButton!
...
let btnDeepPressGestureRecognizer = DeepPressGestureRecognizer(target: self, action: #selector(ViewController.btnDeepPressHandler(_:)), threshold: self.deepPressureThreshold)
btn.addGestureRecognizer(btnDeepPressGestureRecognizer)
...
func btnDeepPressHandler(_ value: DeepPressGestureRecognizer) {
if value.state == .began {
...
} else if value.state == .ended {
...
}
}
其中一个按钮属性检查器的屏幕截图:
我现在只想到两件事:
你可以试试这个:
yourButton.adjustsImageWhenHighlighted = false
还有这个:
yourButton.adjustsImageWhenDisabled = false
根据文档:
When the Shows Touch On Highlight (showsTouchWhenHighlighted) option
is enabled, the button adds a white glow to the part of a button that
the user touches.
When the Highlighted Adjusts Image (adjustsImageWhenHighlighted)
option is enabled, button images get darker when it is in the
highlighted state.
When the Disabled Adjusts Image (adjustsImageWhenDisabled) option is
enabled, the image is dimmed when the button is disabled.
由于它是在轻触时发生的,您的按钮应该突出显示并发光,但它变暗了,所以它可能被禁用了。
在我的 iPhone 应用程序中,我在 Interface Builder
中创建了一堆 UIButton
。每个按钮都有一个 GestureRecognizer
来识别硬压力(3D 触摸),当施加足够的力时,我会更改 UIButton 图像并进行一些计算。
一切正常,唯一的问题是每个按钮在被轻轻触摸(悬停)时都会变成灰色。
如何摆脱这种行为?
以下是我的部分代码:
class ViewController: UIViewController {
@IBOutlet weak var btn: UIButton!
...
let btnDeepPressGestureRecognizer = DeepPressGestureRecognizer(target: self, action: #selector(ViewController.btnDeepPressHandler(_:)), threshold: self.deepPressureThreshold)
btn.addGestureRecognizer(btnDeepPressGestureRecognizer)
...
func btnDeepPressHandler(_ value: DeepPressGestureRecognizer) {
if value.state == .began {
...
} else if value.state == .ended {
...
}
}
其中一个按钮属性检查器的屏幕截图:
我现在只想到两件事:
你可以试试这个:
yourButton.adjustsImageWhenHighlighted = false
还有这个:
yourButton.adjustsImageWhenDisabled = false
根据文档:
When the Shows Touch On Highlight (showsTouchWhenHighlighted) option is enabled, the button adds a white glow to the part of a button that the user touches.
When the Highlighted Adjusts Image (adjustsImageWhenHighlighted) option is enabled, button images get darker when it is in the highlighted state.
When the Disabled Adjusts Image (adjustsImageWhenDisabled) option is enabled, the image is dimmed when the button is disabled.
由于它是在轻触时发生的,您的按钮应该突出显示并发光,但它变暗了,所以它可能被禁用了。