Ios 斯威夫特,按一下按钮,再按一下另一个按钮

Ios 斯威夫特,按一下按钮,再按一下另一个按钮,ios,swift,uibutton,Ios,Swift,Uibutton,我正在制作一个计算器应用程序,它有几个UIButton用于输入数字等。我希望用户能够按下一个按钮,如果这不是预期的按钮,则将手指移动到另一个按钮,并在该按钮内进行触摸。用户手指上的按钮应该改变背景颜色,向用户指示正在发生的事情,就像苹果内置的计算器应用程序一样 我曾尝试通过在按钮上使用“内/外触摸拖动”和“内/外触摸拖动”以及“进入/退出触摸拖动”来实现这一点,但它仅适用于产生触摸的按钮。这意味着我可以触碰一个按钮,向外拖动,再向内拖动,再向内拖动,但我不能触碰,再向外拖动,再向内拖动另一个按钮

我正在制作一个计算器应用程序,它有几个UIButton用于输入数字等。我希望用户能够按下一个按钮,如果这不是预期的按钮,则将手指移动到另一个按钮,并在该按钮内进行触摸。用户手指上的按钮应该改变背景颜色,向用户指示正在发生的事情,就像苹果内置的计算器应用程序一样

我曾尝试通过在按钮上使用“内/外触摸拖动”和“内/外触摸拖动”以及“进入/退出触摸拖动”来实现这一点,但它仅适用于产生触摸的按钮。这意味着我可以触碰一个按钮,向外拖动,再向内拖动,再向内拖动,但我不能触碰,再向外拖动,再向内拖动另一个按钮

此外,被识别为按钮内部或外部的区域大于按钮的边界

下面是我为其中一个按钮尝试的代码示例:

@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
}
任何帮助都将不胜感激

我曾尝试通过在按钮上使用“内/外触摸拖动”和“内/外触摸拖动”以及“进入/退出触摸拖动”来实现这一点,但它仅适用于产生触摸的按钮


绝对正确。触摸“属于”一个视图,只要您继续拖动,它仍然只属于该视图。因此,像您所描述的那样,唯一可能的方法是在这些按钮视图的公共superview中进行触摸检测。

我通过覆盖下面的功能并跟踪最后和倒数第二次触摸哪个按钮,成功创建了一个合适的解决方案。最后一个按钮被高亮显示,倒数第二个按钮被取消高亮显示。以下是我的两个按钮测试代码,以防任何人发现它有用:

@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)
}
@IBOutlet弱var按钮:UIButton!
@IBOutlet弱var topButton:UIButton!
var lastTouchedButton:UIButton?=无
var secondToLastTouchedButton:UIButton?=无
覆盖功能触摸开始(触摸:设置?,withEvent:UIEvent?){
让触摸=触摸?首先
让位置:CGPoint=(touch?.locationInView(self.view))!
if topButton.pointInside(self.view.convertPoint(位置,toView:topButton.viewForLastBaselineLayout),带事件:nil){
topButton?.backgroundColor=UIColor.redColor()
}
else if bottomButton.pointInside(self.view.convertPoint(位置,toView:bottomButton.viewForLastBaselineLayout),带事件:nil){
bottomButton?.backgroundColor=UIColor.redColor()
}
super.touchesbeated(touchs!,with event:event)
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
让我们先接触
让位置:CGPoint=(touch?.locationInView(self.view))!
if topButton.pointInside(self.view.convertPoint(位置,toView:topButton.viewForLastBaselineLayout),带事件:nil){
secondToLastTouchedButton=lastTouchedButton
lastTouchedButton=topButton
lastTouchedButton?.backgroundColor=UIColor.redColor()
}
else if bottomButton.pointInside(self.view.convertPoint(位置,toView:bottomButton.viewForLastBaselineLayout),带事件:nil){
secondToLastTouchedButton=lastTouchedButton
lastTouchedButton=底部按钮
lastTouchedButton?.backgroundColor=UIColor.redColor()
}
否则{
lastTouchedButton?.backgroundColor=UIColor.whiteColor()
}
如果secondToLastTouchedButton!=lastTouchedButton{
secondToLastTouchedButton?.backgroundColor=UIColor.whiteColor()
}
super.touchsmoved(touchs,with event:event)
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
让我们先接触
让位置:CGPoint=(touch?.locationInView(self.view))!
if topButton.pointInside(self.view.convertPoint(位置,toView:topButton.viewForLastBaselineLayout),带事件:nil){
topButton?.backgroundColor=UIColor.whiteColor()
}
else if bottomButton.pointInside(self.view.convertPoint(位置,toView:bottomButton.viewForLastBaselineLayout),带事件:nil){
bottomButton?.backgroundColor=UIColor.whiteColor()
}
super.touchesend(touchs,withEvent:event)
}
覆盖功能触摸已取消(触摸:设置?,带事件:UIEvent?){
lastTouchedButton?.backgroundColor=UIColor.whiteColor()
super.touchscancelled(touchs,withEvent:event)
}

谢谢您的回复!那会使事情变得明朗一点。。我该怎么做呢?如果我有触摸的x,y坐标,我可以检测superview中某个位置的按钮吗?你需要使用点击测试。我的书有一个例子: