【发布时间】:2018-04-04 13:53:39
【问题描述】:
我有一个 UIButton,我希望能够使用 pan gesture 移动它,但无法单击它。我想要的是当用户点击按钮的按钮时不会改变它的外观,就像你只是点击一个视图一样。将isEnabled 设置为false 并将userInteractionIsEnabled 设置为false 会导致按钮均未收到pan events。我可以使用自定义视图来达到预期的效果,但我可以用按钮以某种方式做到这一点。
按要求发布带有按钮创建的代码 sn-p:
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)
}
【问题讨论】:
-
您是否有特殊原因要使用
UIButton而不是UIView? -
我想继续使用 title 和 imageView 属性 - 在按钮还附加一个动作之前,但不再需要它了。但我想这不是一个很好的理由,只是懒惰,我会顺其自然。
-
能提供sn-p的代码吗?
-
@Aditya 提供了用于创建按钮的代码 sn-p
-
您希望 no 按钮在被点击时淡出,但仍接收点击手势和平移手势?
标签: ios uibutton uipangesturerecognizer