AUIButton有一个sent event actontouchDragInside(触按内部拖动)。按钮可以连接到多个操作,例如:

@IBAction func dragButton(sender: UIButton) {
    ...
}

答案应该集中在确定拖动方向,左,右,上,下,在快速编码。
开发人员注释中没有太多信息:https://developer.apple.com/documentation/uikit/uicontrolevents/1618240-touchdraginside
问题:
当我点击并拖动UIButton以执行发送的事件操作touchDragInside时,如何确定实际的拖动方向?

最佳答案

所以,我有一个答案。可能不是最漂亮的代码,但它工作得很好。
做什么:
只需在一个新的Xcode项目中将UIButton放到故事板上。
将下面的Swift 2代码放入ViewController.Swift文件中。
UIButtontouchDowntouchDragInside操作连接到相应的代码。
点击Xcode中的Run►启动模拟器。
应用程序运行后,点击并拖动UIButton以从拖动方向打印。
代码:

//  ViewController.swift

import UIKit

class ViewController: UIViewController {

    var touchDragInsideLocationX: CGFloat!
    var touchDragInsideLocationX1: CGFloat!
    var touchDragInsideLocationX2: CGFloat!
    var touchDragInsideLocationX3: CGFloat!

    var touchDragInsideLocationY: CGFloat!
    var touchDragInsideLocationY1: CGFloat!
    var touchDragInsideLocationY2: CGFloat!
    var touchDragInsideLocationY3: CGFloat!

    @IBAction func touchDownAction(sender: AnyObject, event: UIEvent) {

        // Reset X location points
        touchDragInsideLocationX = 0
        touchDragInsideLocationX1 = 0
        touchDragInsideLocationX2 = 0
        touchDragInsideLocationX3 = 0

        // Reset Y location points
        touchDragInsideLocationY = 0
        touchDragInsideLocationY1 = 0
        touchDragInsideLocationY2 = 0
        touchDragInsideLocationY3 = 0
    }

    @IBAction func touchDragInsideAction(sender: AnyObject, event: UIEvent) {

        // Get current touchDragInside X and Y location points
        let button = sender as? UIButton
        let touch = (event.touchesForView(button!)?.first)! as UITouch
        let location = touch.locationInView(button)
        touchDragInsideLocationX = location.x
        touchDragInsideLocationY = location.y

        // Array previous touchDragInside X location points
        touchDragInsideLocationX3 = touchDragInsideLocationX2
        touchDragInsideLocationX2 = touchDragInsideLocationX1
        touchDragInsideLocationX1 = touchDragInsideLocationX

        // Array previous touchDragInside Y location points
        touchDragInsideLocationY3 = touchDragInsideLocationY2
        touchDragInsideLocationY2 = touchDragInsideLocationY1
        touchDragInsideLocationY1 = touchDragInsideLocationY

        // Determine touchDragInside X and Y velocity
        let touchDragInsideVelocityX = abs(touchDragInsideLocationX - touchDragInsideLocationX3)
        let touchDragInsideVelocityY = abs(touchDragInsideLocationY - touchDragInsideLocationY3)

        // Determine touchDragInside direction
        if touchDragInsideVelocityX > touchDragInsideVelocityY {
            if touchDragInsideLocationX3 != 0 {
                let touchDragInsideVelocityX = (touchDragInsideLocationX3 - touchDragInsideLocationX1)
                if touchDragInsideVelocityX > 0 {touchDragInsideLeft()} else {touchDragInsideRight()}
            }
        } else {
            if touchDragInsideLocationY3 != 0 {
                let touchDragInsideVelocityY = (touchDragInsideLocationY3 - touchDragInsideLocationY1)
                if touchDragInsideVelocityY > 0 {touchDragInsideUp()} else {touchDragInsideDown()}
            }
        }
    }

    func touchDragInsideLeft() {
        print("Left")
    }

    func touchDragInsideRight() {
        print("Right")
    }

    func touchDragInsideUp() {
        print("Up")
    }

    func touchDragInsideDown() {
        print("Down")
    }

}

关于ios - 在UIButton上查找touchDragInside发送事件的方向,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45265303/

10-11 12:28