当前位置: 首页 > 面试题库 >

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

张财
2023-03-14
问题内容

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


 类似资料:
  • 问题内容: (几天前我才刚开始使用Swift,并且对编程还是比较陌生的,所以请耐心等待。)我试图使随机块出现在屏幕上,并且用户必须点按它们以使其消失。我已经能够创建块,但是我不知道如何使它们真正可轻敲。有人可以帮帮我吗?到目前为止,这是我的代码: 编辑: 这是我正在尝试的新代码: 问题答案: 创建视图后,需要将其userInteractionEnabled属性设置为true。然后,您需要向其附加手

  • 我是Salesforce的新手,没有任何编码经验。SF给我留下了深刻的印象,我现在的任务是学习APEX。为了学习APEX,我需要学习java。因此,在努力创建触发器并制作代码示例之后,我通过iTunesU(斯坦福大学免费)注册了一个简介java CS类。 如果我能做一个简单的触发工作,我可以和一个朋友一起成为一个英雄。非常感谢任何帮助。 一旦在联系人记录上保存了选取列表值,工作流规则就会使用联系人

  • 本文向大家介绍iOS轻点、触摸和手势代码开发,包括了iOS轻点、触摸和手势代码开发的使用技巧和注意事项,需要的朋友参考一下 一、响应者链 以UIResponder作为超类的任何类都是响应者。UIView和UIControl是UIReponder的子类,因此所有视图和所有控件都是响应者。 1、初始相应器 事件首先会传递给UIApplication对象,接下来会传递给应用程序的UIWindow,UIW

  • 问题内容: 我无法从另一个类Menu.swift调用GameViewController.swift中的函数。我这样调用该函数: 这是我要调用的函数: } 我在菜单类内的GameViewController.showLeaderboard()行中出现编译器错误“调用中的参数#1缺少参数”,但我不理解编译器期望的参数类型,因为我不需要任何声明就声明了该函数参数。 谢谢 问题答案: 在您定义为方法而不

  • 问题内容: 我试图用选项卡栏模仿UINavigationController的新功能。我已经看到了很多答案,它们要么指向在viewController上设置,仅将其完全隐藏而不是在点击时隐藏。 提前致谢 编辑:根据下面我的建议 实际上确实隐藏了tabBar(在点击时切换true / false),但是没有动画。我将作为一个单独的问题提出。 问题答案: 经过大量的探索并尝试了各种方法来使用Swift

  • GCViewer如果你看一下这个截图,你可以看到几乎是纯蓝色的线,在那里年轻一代的收藏发生迅速连续。