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

2024-02-19

背景

I've 以前学过的 https://stackoverflow.com/a/34764356/3681880如何使用手势识别器或continueTrackingWithTouch获取当前触摸位置的持续更新,然后使用它们执行如下操作:

然而现在,我想学习如何使用目标来做同样的事情。我已经可以通过使用来获得触地和触地事件TouchDown and 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?
}

Notes

  • 得到@CaseyWagner的答案后更改了代码。编译器不再抛出错误,但是UIControlEvents.ValueChanged永远不会被解雇。
  • 这个问题是我试图得到一个完整的答案方法一:添加目标的部分这个答案 https://stackoverflow.com/a/34764356/3681880.

Use 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)"
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 Swift 中的 valueChanged 事件获取触摸坐标 的相关文章

随机推荐

  • JsDoc命名空间

    我正在使用 JsDoc 编辑 js 文件以获得干净的文档 我的结构非常简单 Finds an object param string prop Property Array prototype findObject function fin
  • 将 RGB 颜色存储在 MySQL 中。 char 还是 int 更好?

    我使用 PHP 从 MySQL 数据库查询 CSS 设置 然后将其回显到 CSS 样式表中 示例片段如下
  • ClickOnce 应用程序用户设置位置

    我目前正在开发一个 ClickOnce 应用程序 用于转换 CSV 文件以进行数据库更新 该程序要求用户能够更改数据库更改的配置文件 并更改填充应用程序中下拉列表的 XML 文件 现在我知道这些文件保存在 user appdata 文件夹中
  • 为什么我的 Perl 祝福文件句柄不使用“can('print')”返回 true?

    由于某种原因 我无法让文件句柄与 Expect pm 一起使用log file方法 我最初得到的帮助是如何将文件句柄传递给 Perl Expect 的 log file 函数 https stackoverflow com question
  • 在 pymongo 中快速或批量更新

    如何在 pymongo 中进行批量更新插入 我想更新一堆条目 但一次更新一个非常慢 几乎相同问题的答案在这里 MongoDB 中的批量更新 更新插入 https stackoverflow com questions 4444023 bul
  • 与 CMake 交叉编译时的 Makefile 生成器规范

    我有两件事无法弄清楚 1 使用时CMake GUI 我们可以指定makefile生成器 我将使用 Debian Linux 交叉编译 Raspberry PI 我找不到相关的生成器 我必须选择哪种发电机 我有toolchain rpi cm
  • fscanf 读取最后一个整数两次[重复]

    这个问题在这里已经有答案了 我有以下简单的程序来读取文本文件 num txt 该文本文件每行包含数字 1 2 3 4 5 当我运行该程序时 它打印 5 两次 谁能告诉我为什么会发生这种情况以及如何解决它 提前致谢 int main void
  • 如何创建自动滚动列表

    我正在尝试使用 CSS 动画构建自动滚动列表 我现在得到了什么 players webkit transition opacity 0 5s ease out webkit animation autoScrolling 5s linear
  • Mac Spark-shell 初始化 SparkContext 时出错

    我尝试在 Mac OS Yosemite 10 10 5 上使用启动 Spark 1 6 0 spark 1 6 0 bin hadoop2 4 bin spark shell 它有以下错误 我也尝试安装不同版本的 Spark 但都有相同的
  • d3:在transition.tween() 中使用*name* 参数

    根据文档transition tween https github com mbostock d3 wiki Transitions tween calling transition tween name factory 为指定的对象注册一
  • 如何检查两个日期时间是否在彼此的特定范围内?

    我有两个 datetime64 对象 a 和 b 我想确定它们是否在彼此的特定范围内 然而 范围并不对称 如果a在b的 30到120分钟之间 a比b早半小时到晚2小时之间 则两者在期望的范围内 我的日期时间对象看起来像 m d Y H M
  • Swift 惰性和可选属性

    和有什么区别Lazy or Optional斯威夫特的财产 例如 如果有人正在构建一个从侧面进入的导航栏 我认为这一切都应该在一个内UIViewController 用户可能永远不会打开菜单 但有时他们会 var menu Navigati
  • 在 C++11 中实现递归代理模式

    假设我们有一些Foo允许的对象 cout lt lt myFoo 3 myFoo 5 bar 这需要代理设计模式 斯科特 迈耶斯 Scott Meyers 详细介绍如下 https stackoverflow com questions 2
  • 将 MongoDB 集合移动到另一个集合的更好方法

    在我的网络抓取项目中 我需要将前一天抓取的数据从mongo collection to mongo his collection 我正在使用此查询来移动数据 for record in collection find his collect
  • 我可以在 .gitignore 文件中包含其他 .gitignore 文件吗? (就像类 C 语言中的 #include 一样)

    我有一些文件 例如vim gitignore SVN gitignore and CVS gitignore 散布在我的硬盘上 我可以简单地将这些 gitignore 文件包含在 gitignore文件在新的 Git 项目中吗 Edit 我
  • 空指针的取消引用

    该错误消息到底意味着什么以及如何修复它 空指针的取消引用 这意味着 self 可以通过 initWithStyle reuseIdentifier 的返回值设为 nil 并且后续对默认值的访问将尝试在 nil 地址处读取 因此取消引用 这肯
  • 如何避免包含类实现文件?

    而不是做 include MyClass cpp 我想要做 include MyClass h 我在网上读到过not这样做被认为是不好的做法 简而言之单独编译 首先 让我们举一些简单的例子 struct ClassDeclaration c
  • Flink时间特性和AutoWatermarkInterval

    在 Apache Flink 中 setAutoWatermarkInterval interval 向下游操作员生成水印 以便他们提前事件时间 如果水印在指定的时间间隔内没有更改 没有事件到达 运行时将不会发出任何水印 另一方面 如果在下
  • 如何将列表附加到 pandas 列、系列?

    假设我有以下数据框 d col1 1 2 col2 3 4 df pd DataFrame data d 我想延长col1带数组xtra 然而这会出错 xtra 3 4 df col1 append xtra 我怎样才能追加xtra to
  • 如何从 Swift 中的 valueChanged 事件获取触摸坐标

    背景 I ve 以前学过的 https stackoverflow com a 34764356 3681880如何使用手势识别器或continueTrackingWithTouch获取当前触摸位置的持续更新 然后使用它们执行如下操作 然而