ios - Swift,触摸 UIButton 并触摸另一个

标签 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
}

如有任何帮助,我们将不胜感激!

最佳答案

我设法创建了一个合适的解决方案,方法是覆盖以下函数并跟踪最后一次和倒数第二个触摸的按钮。触摸的最后一个按钮高亮显示,倒数第二个按钮不高亮显示。这是我的两个按钮测试代码,以防有人发现它有用:

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

关于ios - Swift,触摸 UIButton 并触摸另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33614472/

相关文章:

iOS 9 Facebook 访问 token 在未来的应用程序发布中为零

ios - 调用 UIButton.isHidden = true/false 时 View 未更新

objective-c - 使用 Parse.com 查询数组中的 2 个值

ios - 如何响应 UIScrollView 中的 touchupinside 事件?

ios - 按下完成后停止 AV 播放器

ios - 具有动态原型(prototype)内容和多部分的 UITableView

ios - 创建带有图层和可点击边的 3D 立方体 - Objective-C

ios - 导航 Controller 无法正常工作

ios - iOS remotebuild失败,并显示“spawn ideviceinstaller ENOENT”

ios 旋转图像和缩放,所以它适合像 instagram 中的原始框架