如何在 Swift 中识别连续触摸?

2023-12-29

如何在 Swift 代码中识别连续的用户触摸?我所说的连续是指用户将手指放在屏幕上。只要用户触摸屏幕,我就想将精灵套件节点移动到用户触摸的方向。


基本步骤

  1. 存储触摸事件的位置(touchesBegan/touchesMoved)
  2. 将精灵节点移向该位置(update)
  3. 当不再检测到触摸时停止移动节点(touchesEnded)

这是如何执行此操作的示例

Xcode 8

let sprite = SKSpriteNode(color: SKColor.white, size: CGSize(width:32, height:32))
var touched:Bool = false
var location = CGPoint.zero

override func didMove(to view: SKView) {
    /* Add a sprite to the scene */
    sprite.position = CGPoint(x:0, y:0)
    self.addChild(sprite)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    touched = true
    for touch in touches {
        location = touch.location(in:self)
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        location = touch.location(in: self)
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    // Stop node from moving to touch
    touched = false
}

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
    if (touched) {
        moveNodeToLocation()
    }
}

// Move the node to the location of the touch
func moveNodeToLocation() {
    // Compute vector components in direction of the touch
    var dx = location.x - sprite.position.x
    var dy = location.y - sprite.position.y
    // How fast to move the node. Adjust this as needed
    let speed:CGFloat = 0.25
    // Scale vector
    dx = dx * speed
    dy = dy * speed
    sprite.position = CGPoint(x:sprite.position.x+dx, y:sprite.position.y+dy)
}

Xcode 7

let sprite = SKSpriteNode(color: SKColor.whiteColor(), size: CGSizeMake(32, 32))
var touched:Bool = false
var location = CGPointMake(0, 0)

override func didMoveToView(view: SKView) {
    self.scaleMode = .ResizeFill
    /* Add a sprite to the scene */
    sprite.position = CGPointMake(100, 100)
    self.addChild(sprite)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    /* Start moving node to touch location */
    touched = true
    for touch in touches {
        location = touch.locationInNode(self)
    }
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    /* Update to new touch location */
    for touch in touches {
        location = touch.locationInNode(self)
    }
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    // Stop node from moving to touch
    touched = false
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
    if (touched) {
        moveNodeToLocation()
    }
}

// Move the node to the location of the touch
func moveNodeToLocation() {
    // How fast to move the node
    let speed:CGFloat = 0.25
    // Compute vector components in direction of the touch
    var dx = location.x - sprite.position.x
    var dy = location.y - sprite.position.y
    // Scale vector
    dx = dx * speed
    dy = dy * speed
    sprite.position = CGPointMake(sprite.position.x+dx, sprite.position.y+dy)

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

如何在 Swift 中识别连续触摸? 的相关文章

  • Xcode 8:使用 iOS 9.3 基础 SDK 编译?

    我在 Xcode 8 0 beta 8S128d 中将 iOS 应用程序升级到 Swift 3 0 我以为一切都已准备就绪 并将其上传到 iTunes Connect 当我点击 提交审核 时 它给了我一个包含 26 个错误的列表 每个嵌入式
  • 使用 firebase 过滤分页列表

    我正在尝试使用 firebase 和 swift 制作一个分页过滤列表 但请随意用您最喜欢的编程语言回答 而不过滤客户端上检索到的数据 假设我有这个结构 matches match 1 name Match 1 users user 1 o
  • 在 Pages 文稿中打开文本—Swift

    在我的 Swift 2 应用程序中 用户通过文本字段创建一串文本 然后将其共享给另一个应用程序 现在 我只能将文本共享为 txt 文件 这不提供选项Open In Pages当我打开系统共享对话框时 如何才能让用户可以选择将输入的文本作为
  • Swift:Tableview 在导航栏下方滚动但在状态栏上方滚动?

    我使用以下技巧隐藏了导航栏的阴影 self navigationController navigationBar setBackgroundImage UIImage for default self navigationControlle
  • 无法在 xcode 8 beta 6 上编译 AWS CustomIdentityProvider

    我在 iOS 应用程序中使用 Amazon Cognito 和 Facebook 登录 直到 beta 5 为止此代码从这个SO线程 https stackoverflow com questions 37597388 aws cognit
  • Swift PageControl 当前页面上更大的点

    我试图将当前页面的点缩放为大于未 选择 的点 我正在使用滚动视图委托来确定哪个页面是当前的 目前 点的大小没有变化 我将如何实现这一目标 func scrollViewDidEndDecelerating scrollView UIScro
  • 使用 PDFOutline 将 TOC 添加到 Swift/Cocoa 中的 PDFDocument

    我正在开发一个小程序 将多个单页 PDF 合并到一个多页 PDF 中 我正在 Swift4 MacOS Cocoa 中工作 但我一生都无法在 Swift 中找到任何类型的示例来创建大纲 仅遍历现有的大纲 我对此非常熟悉 使用对文档的最佳猜测
  • 自动生成的 Swift 桥接标头中“找不到接口声明”

    我当前的项目包含 Swift 和 Objective C 代码 两种类型的源文件都使用另一种语言的代码 当我进行完全清理并重新编译时 几乎每个 Swift 类声明都出现错误Module Swift h 形式为 Cannot find int
  • didRegisterForRemoteNotificationsWithDeviceToken 停止被调用 [关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我正在测试我的应用程序是否使用 Push Notifs 一整天都好好的 突然就不行了 设置完全没有变化 经过更多调试后 我发现该函
  • Alamofire 仅在 GET 请求上出现请求错误

    我正在努力将我的项目从 AFNetworking 转移到 Alamofire 真的很喜欢这个项目 POST 请求工作得很好 但是 我在尝试发出 GET 请求时收到此错误 这是一些示例代码 class func listCloudCrednt
  • Swift 中使用 T 型进行泛型调用

    在我的应用程序中 我想创建一个通用方法 该方法根据给定类型 T 创建一个对象数组 我创建了以下函数 func getArray
  • 如何使用 SwiftUI 拖动工作滑块

    我想拖动一个滑块 当然也让它滑动 我可以做其中之一 但我不能两者都做 如何拖动并拥有可用的滑块 我也尝试找到一种方法来删除手势 但我找不到方法来做到这一点 还尝试了 Apple Composition SwiftUI Gestures 文档
  • 运行时警告 CLSUserDefaults 实施了两次

    在 cocoapod 源中使用 import 语法时 我在 pod 定义的类中看到了与下面类似的警告 但这是一个内部苹果类定义 CLSUserDefaults 我无法控制它 也不会进行子类化 我不知道这是否真的造成了问题 但它不应该发生 o
  • Braintree DropIn + Apple Pay,未出现 Apple Pay 项目

    我成功调用DropIn来自 Braintree SDK 的视图 这BTDropInRequest设置应显示三项 PayPal 信用卡 苹果支付 但由于某种原因在DropIn视图仅呈现两个项目而不是三个 PayPal 信用卡 我做错了什么 准
  • 将 NSDate 转换为 SWIFT 中具有特定时区的字符串

    在我的核心数据库中 我有一个带有 NSDate 属性的 新闻 实体 我的应用程序遍布全球 新闻已发布2015 09 04 22 15 54 0000法国时间 GMT 2 为了保存日期 我将其转换为 UTC 格式 let mydateForm
  • 错误:“Int”无法转换为“@lvalue Float”

    给定以下函数 func greatestCommonDenominator first Int second Int gt Int return second 0 first greatestCommonDenominator second
  • SwiftUI NavigationView 看不到图像

    我有一个代码并制作 NavigationLink 按钮 我编写文本和图像 但我的图像看不到 请帮助我 VStack Image Coachs resizable aspectRatio contentMode fill frame widt
  • iOS 上每个选项的带有图像的操作表

    有没有办法在 iOS 上将图像添加到操作表中 与苹果在应用程序商店或苹果音乐应用程序上所做的一样 我对苹果文档的基本搜索表明我没有在操作表中子类化或添加子视图 UIActionSheet 并非设计为子类化 也不应向其层次结构添加视图 苹果文
  • 确定手势识别器中触摸的节点

    我有一个 SpriteKit 场景 其中可以有数千个不同的节点 我还在场景中实现了单击手势识别器 希望在触发手势识别器后能够确定场景中哪个节点被触摸 目前 我的 非工作 代码如下所示 objc func singleTap sender U
  • Swift 中的弹出视图

    我有一个弹出视图 没有选项卡栏 它弹出到带有选项卡栏的视图控制器 在带有选项卡栏的视图控制器中 我设置了一个单击按钮 以便弹出视图控制器 IBAction func PopUpClicked sender UIButton gt Void

随机推荐