在3个按钮上滑动手指-Xcode&迅速 [英] Slide finger across 3 buttons - Xcode & Swift

查看:131
本文介绍了在3个按钮上滑动手指-Xcode&迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UI中有3个按钮,它们最终的性能将类似于钢琴上的键.可以轻按这三个按钮中的任何一个来执行特定的按钮动作,但是用户可以将手指滑"到下一个按钮上,以执行按钮2的动作.

I have 3 buttons in my UI that ultimately will perform similar to keys on a piano. Either of the 3 buttons may get tapped to perform that particular buttons actions, but the user can 'slide' their finger off onto the next button to perform button 2's action.

我进行了一些搜索,看起来好像是touchesBegan方法,用于检测点击的位置是最好的.我的尝试如下:

I did some searching and it looks like a touchesBegan method detecting the location of the tap is best. My attempt is below:

@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!

(我没有与这3个按钮相关的任何动作事件,因为我认为touchesBegan可以解决这个问题.)

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    var touch: UITouch = event!.allTouches()!.first!
    var location: CGPoint = touch.locationInView(touch.view!)
    if CGRectContainsPoint(button1.frame, location){
        print("button 1")
    }
    else if CGRectContainsPoint(button2.frame, location) {
        print("button 2")
    }
    else if CGRectContainsPoint(button3.frame, location) {
        print("button 3")
    }

}

当我对此进行测试时,什么都不会打印到控制台.我测试了在按钮之间的点击和滑动.我应该使用UIViews代替UIButtons吗?我应该在按钮上使用动作吗?这是针对Swift 2.0的.谢谢.

Nothing prints to the console when I test this. I tested both tapping and swiping across buttons. Should I be using UIViews instead of UIButtons? Should I be using actions on the buttons? This is for Swift 2.0. Thank you.

编辑

我有一个工作原型,非常接近我对这种功能的设想.此线程位于:使用iOS平移手势来突出显示网格中的按钮指向我在按钮超级视图上使用手势的方向.

I have a working prototype much closer to how I envisioned this functioning. This thread here: Using iOS pan gesture to highlight buttons in grid pointed me in the direction of using a pangesture on the buttons superview.

var touchPoint = CGPoint()
var myGesture = UIPanGestureRecognizer()

viewDidLoad:

viewDidLoad:

    myGesture = UIPanGestureRecognizer(target: self, action: Selector("panDetected:"))
    myGesture.maximumNumberOfTouches = 1
    view.addGestureRecognizer(myGesture)

.

func panDetected(sender : UIPanGestureRecognizer) {
    touchPoint = sender.locationInView(self.view)


        if CGRectContainsPoint(button1.frame, touchPoint) {
            button1Func()
        }

        else if CGRectContainsPoint(button2.frame, touchPoint) {
            button2Func()
        }

        else if CGRectContainsPoint(button3.frame, touchPoint) {
            button3Func()
        }

上面的方法确实起作用,但是button1Func/button2Func/button3Func都包含一个动画块.当手指在按钮内拖动时,每次检测到移动时都会触发该方法.我只需要让这件事发生一次.我尝试将其添加到state == .Began语句中,但是一旦手指离开第一个轻按按钮,该功能将无法使用.

The above DOES work, however, button1Func / button2Func / button3Func all contain an animation block. When the finger is dragged within the buttons, it fires the method every time a movement is detected. I just need for this to happen once. I tried adding this to a state == .Began statement, but that prevents any functionality once the finger leaves the first tapped button.

一旦手指位于3个按钮中的每个按钮的边界内,是否可以在平移手势中一次触发这些方法(button1Func/button2Func/button3Func)?如果这令人困惑,我将很乐意澄清.谢谢.

Is there any way I can fire those methods (button1Func / button2Func / button3Func) just ONCE inside the pan gesture once the finger is inside the bounds of each of the 3 buttons? I will be happy to clarify this if it's confusing. THANKS.

推荐答案

您可以将许多控制事件绑定到IBAction.看一下UIControlEvents枚举.您可能想为UIControlEventTouchDragInsideUIControlEventTouchDragEnter和其他几个动作添加

There are a whole bunch of control events you can tie to IBActions. Take a look at the UIControlEvents enum. You probably want to add actions for UIControlEventTouchDragInside, UIControlEventTouchDragEnter and a few others.

这篇关于在3个按钮上滑动手指-Xcode&amp;迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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