小编典典

迅速,轻触UIButton并轻触另一个

swift

我正在制作一个具有几个用于输入数字等的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
}

任何帮助将非常感激!


阅读 382

收藏
2020-07-07

共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)
}
2020-07-07