【问题标题】:How to get the touch coordinates from a valueChanged event in Swift如何从 Swift 中的 valueChanged 事件中获取触摸坐标
【发布时间】:2016-01-13 09:56:24
【问题描述】:

背景

我已经 previously learned 如何使用手势识别器或 continueTrackingWithTouch 获取当前触摸位置的持续更新,然后使用它们来执行以下操作:

不过,现在我想学习如何使用目标来做同样的事情。我已经可以通过使用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 永远不会被触发。
  • 这个问题是我试图对this answer方法1:添加目标部分有一个完整的答案。

【问题讨论】:

    标签: ios swift uicontrolevents


    【解决方案1】:

    使用UIControlEvents.TouchDragInside而不是UIControlEvents.ValueChanged(还要注意action方法接收两个参数):

    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 = ...,因为我不确定强制展开是否会导致崩溃。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
      1. AppDelegate 中评论@UIApplicationMain
      2. 创建名为 main.swift 的新文件并将此代码添加到该文件中

        import UIKit
        import Foundation
        UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(AppClass), NSStringFromClass(AppDelegate))
        
      3. 创建名为“AppClass”的新类和 UIApplication 的子类

      4. 在该方法中编写以下代码 导入 UIKit

        class AppClass: UIApplication {
            override func sendEvent(event: UIEvent)
            {
                super.sendEvent(event)
                print("send event") // this is an example
                // ... dispatch the message...
             }   
        }
        
      5. 在这里,在 sendEvent 方法中,您将获得应用程序中发生的所有事件(所有屏幕)、所有触摸事件,因此您可以在这里使用此 UIEvent 并根据需要执行任何任务

        李>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-28
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        • 1970-01-01
        • 2011-10-05
        • 1970-01-01
        相关资源
        最近更新 更多