簡體   English   中英

在按住UIButton時移除陰影

[英]Removing the shadow on a UIButton while it's being held

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM