如何使UIButton可响应平移手势但不可点击且无点击外观变化
当然可以直接用UIButton实现这个需求!不用自定义全新的视图,只需要对UIButton做一些小改造就能达到效果——核心是保留按钮的交互能力(让它能接收平移手势),同时拦截点击事件和点击时的视觉状态变化。下面是具体的实现方案:
核心思路
因为isEnabled = false或userInteractionEnabled = false会直接关闭所有交互(包括手势),所以我们得换个思路:
- 保持
userInteractionEnabled = true,让按钮能正常接收触摸和手势事件 - 拦截按钮的点击事件响应,不让它触发原本的点击逻辑
- 阻止按钮进入高亮(highlighted)状态,避免点击时出现默认的视觉变化(比如变暗、缩小等)
通过继承UIButton重写关键方法,这种方式最彻底,也方便在多个地方复用。
首先定义自定义按钮类:
class DraggableNonClickableButton: UIButton { // 拦截点击事件:不让按钮发送点击动作给目标对象 override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) { // 空实现,直接阻断点击事件传递 } // 强制禁用高亮状态:避免点击时的视觉反馈 override var isHighlighted: Bool { didSet { super.isHighlighted = false } } // 初始化时的通用配置,双重保险防止视觉变化 private func commonInit() { // 关闭图片在高亮/禁用时的自动调整 adjustsImageWhenHighlighted = false adjustsImageWhenDisabled = false // 确保标题在所有状态下颜色一致(如果用了自定义标题色) setTitleColor(.systemBlue, for: .normal) setTitleColor(.systemBlue, for: .highlighted) setTitleColor(.systemBlue, for: .disabled) } override init(frame: CGRect) { super.init(frame: frame) commonInit() } required init?(coder: NSCoder) { super.init(coder: coder) commonInit() } }
然后是创建按钮并添加平移手势的代码片段:
func createDraggableButton() { // 实例化自定义按钮 let draggableButton = DraggableNonClickableButton(type: .system) draggableButton.setTitle("可拖动无点击按钮", for: .normal) draggableButton.frame = CGRect(x: 80, y: 200, width: 200, height: 60) draggableButton.backgroundColor = .lightGray draggableButton.layer.cornerRadius = 8 // 添加平移手势 let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:))) draggableButton.addGestureRecognizer(panGesture) // 添加到视图 view.addSubview(draggableButton) } // 处理平移手势的方法 @objc private func handlePanGesture(_ gesture: UIPanGestureRecognizer) { guard let button = gesture.view else { return } let translation = gesture.translation(in: view) // 更新按钮位置 button.center = CGPoint( x: button.center.x + translation.x, y: button.center.y + translation.y ) // 重置手势的偏移量,避免累积 gesture.setTranslation(.zero, in: view) }
方案二:不自定义子类的快速实现(适合临时场景)
如果不想自定义子类,也可以通过设置按钮属性+拦截触摸事件来实现,不过不如子类方案彻底:
func createDraggableButtonWithoutSubclass() { let button = UIButton(type: .system) button.setTitle("可拖动无点击按钮", for: .normal) button.frame = CGRect(x: 80, y: 200, width: 200, height: 60) button.backgroundColor = .lightGray button.layer.cornerRadius = 8 // 关闭高亮时的视觉变化 button.adjustsImageWhenHighlighted = false button.adjustsImageWhenDisabled = false button.setTitleColor(.systemBlue, for: .normal) button.setTitleColor(.systemBlue, for: .highlighted) // 拦截点击事件:给按钮添加一个优先级更高的点击手势,什么都不做 let tapGesture = UITapGestureRecognizer(target: self, action: #selector(doNothing)) tapGesture.cancelsTouchesInView = true button.addGestureRecognizer(tapGesture) // 添加平移手势 let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:))) button.addGestureRecognizer(panGesture) view.addSubview(button) } @objc private func doNothing() { // 空实现,拦截点击 } @objc private func handlePanGesture(_ gesture: UIPanGestureRecognizer) { guard let button = gesture.view else { return } let translation = gesture.translation(in: view) button.center = CGPoint(x: button.center.x + translation.x, y: button.center.y + translation.y) gesture.setTranslation(.zero, in: view) }
注意:这种临时方案存在小缺陷——如果用户长按按钮,还是可能出现高亮状态,所以自定义子类的方案更稳妥。
内容的提问来源于stack exchange,提问作者surToTheW
相关产品推荐
相关产品推荐

