如何在Swift中从valueChanged事件获取触摸坐标

10

背景

之前学过如何使用手势识别器或 continueTrackingWithTouch 来连续更新当前触摸位置,并将其用于执行类似以下操作的操作:

enter image description here

现在,我想学习如何使用目标来完成同样的事情。我已经可以通过使用TouchDownTouchUpInside获取触摸按下和触摸抬起事件,但我不知道如何获取连续更新。我假设应该使用ValueChanged事件,但到目前为止还没有起作用。 这是我尝试过的:

ViewController.swift

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var myCustomControl: MyCustomControl!
    @IBOutlet weak var trackingBeganLabel: UILabel!
    @IBOutlet weak var trackingEndedLabel: UILabel!
    @IBOutlet weak var xLabel: UILabel!
    @IBOutlet weak var yLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // these work
        myCustomControl.addTarget(self, action: "myTouchDown", forControlEvents: UIControlEvents.TouchDown)
        myCustomControl.addTarget(self, action: "myTouchUpInside", forControlEvents: UIControlEvents.TouchUpInside)

        // this doesn't work
        myCustomControl.addTarget(self, action: "myValueChangedEvent:",
            forControlEvents: UIControlEvents.ValueChanged)
    }

    // this works
    func myTouchDown() {
        trackingBeganLabel.text = "Touched down"
    }

    // this works
    func myTouchUpInside() {
        trackingEndedLabel.text = "Touch up inside"
    }

    // this doesn't work, function never gets called
    func myValueChangedEvent(sender: UIControl) { 
        let location = sender.convertPoint(CGPointZero, toView: myCustomControl) 
        xLabel.text = "x: \(location.x)"
        yLabel.text = "y: \(location.y)"
    }
}

MyCustomControl.swift

import UIKit
class MyCustomControl: UIControl {
    // currently empty. Do I need to add something here?
}

注意事项

  • 在得到@CaseyWagner的答案后更改了代码。编译器不再抛出错误,但UIControlEvents.ValueChanged从未被触发。
  • 这个问题是我尝试为这个答案中的方法1:添加目标部分提供完整答案。
3个回答

11

使用UIControlEvents.TouchDragInside替代UIControlEvents.ValueChanged(同时注意操作方法接收两个参数):

myCustomControl.addTarget(self, action: "didDragInsideControl:withEvent:",
        forControlEvents: UIControlEvents.TouchDragInside)

处理程序函数的样子是这样的:

func didDragInsideControl(control: MyCustomControl, withEvent event: UIEvent) {
    let touch = event.touchesForView(control)!.first!
    let location = touch.locationInView(control)
    xLabel.text = "x: \(location.x)"
    yLabel.text = "y: \(location.y)"
}

太好了!我错过了“TouchDragInside”事件。我使用了if let touch = ...,因为我不确定强制解包是否会导致崩溃。 - Suragch

1

没有看到你的其他代码,看起来你可能需要:

 let location = sender.convertPoint(CGPointZero, toView: myCustomControl)

1
  1. Comment @UIApplicationMain line in AppDelegate
  2. Create new file with name main.swift and add this code in that file

    import UIKit
    import Foundation
    UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(AppClass), NSStringFromClass(AppDelegate))
    
  3. Create New class with name "AppClass" and subclass of UIApplication

  4. Write following code in that method import UIKit

    class AppClass: UIApplication {
        override func sendEvent(event: UIEvent)
        {
            super.sendEvent(event)
            print("send event") // this is an example
            // ... dispatch the message...
         }   
    }
    
  5. Here, in sendEvent method, you will get all the events happening in application(all screens), all touch events, so here you can use this UIEvent and perform any task if you want


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接