gpt4 book ai didi

ios - 移除按住 UIButton 时的阴影

转载 作者:行者123 更新时间:2023-11-28 12:11:09 26 4
gpt4 key购买 nike

我有一个 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
})
}
}

然而,这似乎并没有触发任何东西。影子一直活在按钮下面。我在这里遗漏了什么吗?

谢谢。

最佳答案

您的手势识别器正在被按钮的选择器覆盖。在您的场景中,最好在选择按钮时覆盖按钮并隐藏其阴影。

class ShadowButton: UIButton {
override var isHighlighted: Bool {
didSet {
UIView.animate(withDuration: 0.1) {
self.layer.shadowOpacity = self.isHighlighted ? 0 : 0.15
}
}
}
}

关于ios - 移除按住 UIButton 时的阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48501170/

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