斯威夫特,触及UIButton并触及另一个

时间:2015-11-09 17:14:01

标签: ios swift uibutton

我正在制作一个计算器应用程序,其中有几个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
}

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

我设法通过覆盖下面的功能创建一个合适的解决方案,并跟踪最后触摸的按钮和倒数第二个按钮。触摸的最后一个按钮会突出显示,倒数第二个按钮会突出显示。这是我的双键测试代码,以防任何人发现它有用:

@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)
}

答案 1 :(得分:1)

  

我尝试通过在内部/外部使用触摸拖动并触摸按钮上的拖动进入/退出来尝试执行此操作,但它仅适用于触摸源自的按钮

绝对正确。触摸&#34;属于&#34;到视图,只要你继续拖动,它仍然只属于该视图。因此,只有像你描述的那样才能实现触摸检测才能在这些按钮视图的公共超级视图中进行