我有一个UIButton
突出显示状态涉及删除阴影。我尝试做的是在按钮上加UILongPressGestureRecognizer
:
let gesture = UILongPressGestureRecognizer(target: self, action: #selector(self.removeShadow))
gesture.numberOfTapsRequired = 1
gesture.numberOfTouchesRequired = 1
gesture.delaysTouchesBegan = false
gesture.delaysTouchesEnded = false
gesture.minimumPressDuration = 0.01
self.addGestureRecognizer(gesture)
然后在我的行动中,我使用状态隐藏并显示阴影:
@objc func removeShadow(gesture: UILongPressGestureRecognizer) {
if gesture.state == .recognized {
UIView.animate(withDuration: 0.1, animations: {
self.layer.shadowOpacity = 0
})
} else if gesture.state == .ended {
UIView.animate(withDuration: 0.1, animations: {
self.layer.shadowOpacity = 0.15
})
}
}
然而,这似乎并没有触发任何事情。影子一直生活在按钮下。我在这里错过了什么吗?
感谢。
答案 0 :(得分:2)
按钮的选择器正在覆盖您的手势识别器。在您的场景中,覆盖按钮并在选中时隐藏其阴影会更好。
class ShadowButton: UIButton {
override var isHighlighted: Bool {
didSet {
UIView.animate(withDuration: 0.1) {
self.layer.shadowOpacity = self.isHighlighted ? 0 : 0.15
}
}
}
}