如何从 Swift 中的 valueChanged 事件获取触摸坐标
How to get the touch coordinates from a valueChanged event in Swift
背景
我已经 previously learned 如何使用手势识别器或 continueTrackingWithTouch
获取当前触摸位置的持续更新,然后使用它们来执行如下操作:
但是,现在我想学习如何使用目标来做同样的事情。我已经可以使用 TouchDown
和 TouchUpInside
获取触地和触地事件,但我不知道如何获取持续更新。我假设它会使用 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:添加目标 部分获得完整答案的尝试。
没有看到您的其余代码,看起来您可能需要:
let location = sender.convertPoint(CGPointZero, toView: myCustomControl)
使用 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)"
}
- 在
AppDelegate
中注释 @UIApplicationMain
行
创建名为 main.swift 的新文件并在该文件中添加此代码
import UIKit
import Foundation
UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(AppClass), NSStringFromClass(AppDelegate))
使用 UIApplication
的名称 "AppClass" 和子 class 创建新的 class
在该方法中写入如下代码
导入 UIKit
class AppClass: UIApplication {
override func sendEvent(event: UIEvent)
{
super.sendEvent(event)
print("send event") // this is an example
// ... dispatch the message...
}
}
在这里,在 sendEvent 方法中,您将获得应用程序(所有屏幕)中发生的所有事件,所有触摸事件,因此您可以在这里使用此 UIEvent 并根据需要执行任何任务
背景
我已经 previously learned 如何使用手势识别器或 continueTrackingWithTouch
获取当前触摸位置的持续更新,然后使用它们来执行如下操作:
但是,现在我想学习如何使用目标来做同样的事情。我已经可以使用 TouchDown
和 TouchUpInside
获取触地和触地事件,但我不知道如何获取持续更新。我假设它会使用 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:添加目标 部分获得完整答案的尝试。
没有看到您的其余代码,看起来您可能需要:
let location = sender.convertPoint(CGPointZero, toView: myCustomControl)
使用 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)"
}
- 在
AppDelegate
中注释 创建名为 main.swift 的新文件并在该文件中添加此代码
import UIKit import Foundation UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(AppClass), NSStringFromClass(AppDelegate))
使用 UIApplication
的名称 "AppClass" 和子 class 创建新的 class
在该方法中写入如下代码 导入 UIKit
class AppClass: UIApplication { override func sendEvent(event: UIEvent) { super.sendEvent(event) print("send event") // this is an example // ... dispatch the message... } }
在这里,在 sendEvent 方法中,您将获得应用程序(所有屏幕)中发生的所有事件,所有触摸事件,因此您可以在这里使用此 UIEvent 并根据需要执行任何任务
@UIApplicationMain
行