斯威夫特,触摸 UIButton 并触摸另一个
Posted
技术标签:
【中文标题】斯威夫特,触摸 UIButton 并触摸另一个【英文标题】:Swift, touch down on UIButton and touch up on another 【发布时间】:2015-11-09 17:14:00 【问题描述】:我正在制作一个计算器应用程序,它有几个 UIButtons 用于输入数字等。我希望用户能够按下一个按钮,如果这不是预期的按钮,请将手指移动到另一个按钮并在那个里面补一下。用户手指按下的按钮应更改背景颜色以向用户指示正在发生的事情,就像苹果内置的计算器应用程序一样。
我尝试通过使用内部/外部触摸拖动和按钮上的触摸拖动进入/退出来做到这一点,但它仅适用于触摸起源的按钮。这意味着我可以触摸一个按钮,向外拖动,向内拖动并向上触摸,但我不能触摸,向外拖动并向上触摸另一个按钮。
此外,被识别为在按钮内部或外部的区域大于按钮的边界。
这是我为其中一个按钮尝试过的代码示例:
@IBAction func didTouchDownThreeButton(sender: AnyObject)
threeButton.backgroundColor = blueColor
@IBAction func didTouchUpInsideThreeButton(sender: AnyObject)
inputTextView.text = inputTextView.text + "3"
threeButton.backgroundColor = lightGrayColor
@IBAction func didTouchDragExitThreeButton(sender: AnyObject)
threeButton.backgroundColor = lightGrayColor
@IBAction func didTouchDragEnterThreeButton(sender: AnyObject)
threeButton.backgroundColor = blueColor
任何帮助将不胜感激!
【问题讨论】:
【参考方案1】:我设法创建了一个合适的解决方案,方法是覆盖下面的函数并跟踪最后触摸哪个按钮和倒数第二个。最后触摸的按钮会突出显示,倒数第二个按钮不会突出显示。这是我的两个按钮测试代码,以防任何人发现它有用:
@IBOutlet weak var bottomButton: UIButton!
@IBOutlet weak var topButton: UIButton!
var lastTouchedButton: UIButton? = nil
var secondToLastTouchedButton: UIButton? = nil
override func touchesBegan(touches: Set<UITouch>?, withEvent event: UIEvent?)
let touch = touches?.first
let location : CGPoint = (touch?.locationInView(self.view))!
if topButton.pointInside(self.view.convertPoint(location, toView: topButton.viewForLastBaselineLayout), withEvent: nil)
topButton?.backgroundColor = UIColor.redColor()
else if bottomButton.pointInside(self.view.convertPoint(location, toView: bottomButton.viewForLastBaselineLayout), withEvent: nil)
bottomButton?.backgroundColor = UIColor.redColor()
super.touchesBegan(touches!, withEvent:event)
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)
let touch = touches.first
let location : CGPoint = (touch?.locationInView(self.view))!
if topButton.pointInside(self.view.convertPoint(location, toView: topButton.viewForLastBaselineLayout), withEvent: nil)
secondToLastTouchedButton = lastTouchedButton
lastTouchedButton = topButton
lastTouchedButton?.backgroundColor = UIColor.redColor()
else if bottomButton.pointInside(self.view.convertPoint(location, toView: bottomButton.viewForLastBaselineLayout), withEvent: nil)
secondToLastTouchedButton = lastTouchedButton
lastTouchedButton = bottomButton
lastTouchedButton?.backgroundColor = UIColor.redColor()
else
lastTouchedButton?.backgroundColor = UIColor.whiteColor()
if secondToLastTouchedButton != lastTouchedButton
secondToLastTouchedButton?.backgroundColor = UIColor.whiteColor()
super.touchesMoved(touches, withEvent: event)
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?)
let touch = touches.first
let location : CGPoint = (touch?.locationInView(self.view))!
if topButton.pointInside(self.view.convertPoint(location, toView: topButton.viewForLastBaselineLayout), withEvent: nil)
topButton?.backgroundColor = UIColor.whiteColor()
else if bottomButton.pointInside(self.view.convertPoint(location, toView: bottomButton.viewForLastBaselineLayout), withEvent: nil)
bottomButton?.backgroundColor = UIColor.whiteColor()
super.touchesEnded(touches, withEvent: event)
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?)
lastTouchedButton?.backgroundColor = UIColor.whiteColor()
super.touchesCancelled(touches, withEvent: event)
【讨论】:
【参考方案2】:我尝试通过在按钮上使用触摸拖动内部/外部和触摸拖动进入/退出来做到这一点,但它仅适用于触摸起源的按钮
完全正确。触摸“属于”一个视图,只要你继续拖动,它仍然只属于那个视图。因此,像您所描述的那样的唯一方法是触摸检测发生在这些按钮视图的公共超级视图中。
【讨论】:
感谢您的回复!这让事情有点清楚了。我该怎么做呢?如果我有触摸的 x,y 坐标,我可以检测到超级视图中某个位置的按钮是什么? 您需要使用命中测试。我的书有一个例子:github.com/mattneub/Programming-ios-Book-Examples/blob/master/…以上是关于斯威夫特,触摸 UIButton 并触摸另一个的主要内容,如果未能解决你的问题,请参考以下文章
UIButton 没有响应嵌套在另一个 UIScrollView 中的 UIScrollView 中的触摸