UIButton 不可点击但可接收手势
UIButton to not be clickable but to receive gestures
我有一个 UIButton,我希望它可以随 pan gesture
一起移动,但无法单击它。我想要的是当用户点击按钮时按钮不会改变其外观,就像您只是点击一个视图一样。将 isEnabled
设置为 false
并将 userInteractionIsEnabled
设置为 false
都会导致按钮未收到 pan events
。我可以通过自定义视图实现所需的效果,但我可以通过按钮以某种方式实现吗?
根据要求发布带有按钮创建的代码片段:
func createAnswerButton() {
let width : CGFloat = 154
let height : CGFloat = 60
let originX = (view.frame.size.width - width) / 2
let originY = startOverButton.frame.origin.y + collectionView.frame.size.height
let frame = CGRect(x: originX, y: originY, width: width, height: height)
let button = UIButton(frame: frame)
answerButton = button
button.backgroundColor = .clear
self.answerButton.setTitleColor(.white, for: .normal)
let image = UIImage(named: "item_neutral")
button.setBackgroundImage(image, for: .normal)
view.addSubview(button)
let panGesture = UIPanGestureRecognizer(target: self, action: (#selector(MoviesViewController.didPanButton(_:))))
button.addGestureRecognizer(panGesture)
}
几乎你必须说按钮更喜欢一个手势而不是另一个
例如:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// Don't recognize a single tap until a double-tap fails.
if gestureRecognizer == self.tapGesture &&
otherGestureRecognizer == self.doubleTapGesture {
return true
}
return 错误
}
有关更多信息,请关注 link:
试试这个,
func createAnswerButton() {
let width : CGFloat = 154
let height : CGFloat = 60
let originX = (view.frame.size.width - width) / 2
let originY = startOverButton.frame.origin.y + collectionView.frame.size.height
let frame = CGRect(x: originX, y: originY, width: width, height: height)
let button = UIButton(frame: frame)
answerButton = button
button.backgroundColor = .clear
self.answerButton.setTitleColor(.white, for: .normal)
let image = UIImage(named: "item_neutral")
button.setBackgroundImage(image, for: .normal)
button.addTarget(self, action: #selector(self.buttonClicked(_:)), for: .touchUpInside)
view.addSubview(button)
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture))
button.addGestureRecognizer(panGesture)
}
@objc func handlePanGesture(sender: UIPanGestureRecognizer? = nil) {
print("Pan Gesture detected")
var buttonCenter : CGPoint = button.center
if sender?.state == .began {
buttonCenter = button.center
} else if sender?.state == .ended || sender?.state == .failed || sender?.state == .cancelled {
button.center = buttonCenter
} else {
let location = sender?.location(in: view)
button.center = location!
}
print("\(buttonCenter)")
}
@objc func buttonClicked(_ sender: UIButton){
print("Button Click detected")
}
为此,您必须将按钮的 adjustsImageWhenHighlighted
设置为 false
(如果您想以编程方式进行)。在界面生成器中,您可以将按钮 type
更改为 custom
。
这是以编程方式执行此操作的代码:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let width : CGFloat = 154
let height : CGFloat = 60
let originX = (view.frame.size.width - width) / 2
let originY = (view.frame.size.height - width) / 2
let frame = CGRect(x: originX, y: originY, width: width, height: height)
let button = UIButton(type: .custom)
button.adjustsImageWhenHighlighted = false
button.frame = frame
button.addTarget(self, action: #selector(buttonInCodeTapped), for: .touchUpInside)
button.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(panForButtonInCode(recognizer:))))
button.setBackgroundImage(UIImage(named: "X"), for: .normal)
view.addSubview(button)
}
这是 属性 设置为 false
时的结果:
这是 属性 设置为 true
时的结果(您可以看到,点击时 X
会突出显示):
我有一个 UIButton,我希望它可以随 pan gesture
一起移动,但无法单击它。我想要的是当用户点击按钮时按钮不会改变其外观,就像您只是点击一个视图一样。将 isEnabled
设置为 false
并将 userInteractionIsEnabled
设置为 false
都会导致按钮未收到 pan events
。我可以通过自定义视图实现所需的效果,但我可以通过按钮以某种方式实现吗?
根据要求发布带有按钮创建的代码片段:
func createAnswerButton() {
let width : CGFloat = 154
let height : CGFloat = 60
let originX = (view.frame.size.width - width) / 2
let originY = startOverButton.frame.origin.y + collectionView.frame.size.height
let frame = CGRect(x: originX, y: originY, width: width, height: height)
let button = UIButton(frame: frame)
answerButton = button
button.backgroundColor = .clear
self.answerButton.setTitleColor(.white, for: .normal)
let image = UIImage(named: "item_neutral")
button.setBackgroundImage(image, for: .normal)
view.addSubview(button)
let panGesture = UIPanGestureRecognizer(target: self, action: (#selector(MoviesViewController.didPanButton(_:))))
button.addGestureRecognizer(panGesture)
}
几乎你必须说按钮更喜欢一个手势而不是另一个
例如:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// Don't recognize a single tap until a double-tap fails.
if gestureRecognizer == self.tapGesture &&
otherGestureRecognizer == self.doubleTapGesture {
return true
} return 错误 }
有关更多信息,请关注 link:
试试这个,
func createAnswerButton() {
let width : CGFloat = 154
let height : CGFloat = 60
let originX = (view.frame.size.width - width) / 2
let originY = startOverButton.frame.origin.y + collectionView.frame.size.height
let frame = CGRect(x: originX, y: originY, width: width, height: height)
let button = UIButton(frame: frame)
answerButton = button
button.backgroundColor = .clear
self.answerButton.setTitleColor(.white, for: .normal)
let image = UIImage(named: "item_neutral")
button.setBackgroundImage(image, for: .normal)
button.addTarget(self, action: #selector(self.buttonClicked(_:)), for: .touchUpInside)
view.addSubview(button)
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture))
button.addGestureRecognizer(panGesture)
}
@objc func handlePanGesture(sender: UIPanGestureRecognizer? = nil) {
print("Pan Gesture detected")
var buttonCenter : CGPoint = button.center
if sender?.state == .began {
buttonCenter = button.center
} else if sender?.state == .ended || sender?.state == .failed || sender?.state == .cancelled {
button.center = buttonCenter
} else {
let location = sender?.location(in: view)
button.center = location!
}
print("\(buttonCenter)")
}
@objc func buttonClicked(_ sender: UIButton){
print("Button Click detected")
}
为此,您必须将按钮的 adjustsImageWhenHighlighted
设置为 false
(如果您想以编程方式进行)。在界面生成器中,您可以将按钮 type
更改为 custom
。
这是以编程方式执行此操作的代码:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let width : CGFloat = 154
let height : CGFloat = 60
let originX = (view.frame.size.width - width) / 2
let originY = (view.frame.size.height - width) / 2
let frame = CGRect(x: originX, y: originY, width: width, height: height)
let button = UIButton(type: .custom)
button.adjustsImageWhenHighlighted = false
button.frame = frame
button.addTarget(self, action: #selector(buttonInCodeTapped), for: .touchUpInside)
button.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(panForButtonInCode(recognizer:))))
button.setBackgroundImage(UIImage(named: "X"), for: .normal)
view.addSubview(button)
}
这是 属性 设置为 false
时的结果:
这是 属性 设置为 true
时的结果(您可以看到,点击时 X
会突出显示):