斯威夫特,触及UIButton并触及另一个 [英] Swift, touch down on UIButton and touch up on another

查看:104
本文介绍了斯威夫特,触及UIButton并触及另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making a calculator app that has several UIButtons for input of digits etc. I want the user to be able to touch down on one button and, if this was not the intended button, move the finger to another button and touch up inside that one. The button that the user has his/her finger on should change background color to indicate to the user what is happening, much like Apples built in calculator app.

我是尝试通过在内部/外部使用触摸拖动并触摸按钮上的拖动进入/退出来执行此操作,但它仅适用于触摸源自的按钮。意思是我可以触摸一个按钮,向外拖动,向内滑动并向内触摸,但我无法触及,向外拖动并在另一个按钮内触摸。

I've tried to do this by using touch drag inside/outside and touch drag enter/exit on the buttons, but it only works for the button where the touch originated. Meaning I can touch down on one button, drag outside, back inside and touch up inside, but I can't touch down, drag outside and touch up inside another button.

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

Also the area that is recognized as being inside or outside the button is larger than the bounds of the button.

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

Here's an example of the code I've tried for one of the buttons:

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

非常感谢任何帮助!

推荐答案

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

I managed to create a suitable solution by overriding the functions below and keeping track of which button was touched last and second to last. The last button touched gets highlighted and the second to last gets unhighlighted. Here's my code for a two button test in case anyone should find it useful:

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

这篇关于斯威夫特,触及UIButton并触及另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆