ios - 移除按住 UIButton 时的阴影

标签 ios swift uibutton

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

相关文章:

ios - UIPickerView Swift 上奇怪的自定义背景颜色

ios - 如何禁用自定义按钮上的 iOS 默认光泽效果

ios - 如何垂直放置UIStepper?

Swift 三个双引号

ios - 使用 UITextField 输入更新 segues 之间的 proximityUUID.UUIDString

ios - 使用参数初始化 UIButton

ios - 在 Swift 中使用 UIButton 动画

ios - 更改单个单元格 UITableView 的高度

ios - Swift 项目中的 Swift Pod

ios - 我可以拥有一个部分在 Swift 中实现,部分在 Objective C 中实现的类吗?