- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
更新[11/1/2018]:
我尝试一步步测试代码,发现:
我正在关注有关实现多点触控应用程序的 Apple 文档。
我正在使用 4 种方法: - touchesBegan(:with:), - touchesMoved(:with:), - touchesEnded(:with:), - touchesCancelled(:with:)
在调用 touchesBegan 时在触摸位置创建一个 UIView。调用 touchesMoved 时更新 UIView 位置。最后,在调用 touchesEnded 和 touchedCancelled 时移除 UIView。
问题是当我在屏幕上快速点击时,UIView 不会从 super View 中移除。代码与网址中提供的代码基本相同,只是图形不同。我添加了另一个 View ,它稍微偏离触摸位置,以便更好地查看触摸点。
Example of the leftover UIView
Another Image, same code as the comment below
我不明白哪里出了问题,为什么会这样。我怀疑从未调用过 touchesEnded() 但我不知道为什么。我想问一下有没有什么方法可以防止这种情况发生(没有剩余的 UIView)?
我正在使用 XCode 版本 9.2 (9C40b) 和 iPad Air 2。我在不同的 iPad 型号上试用了该应用程序,但同样的事情发生了。
部分代码:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
createViewForTouch(touch: touch)
}
}
func createViewForTouch( touch : UITouch ) {
let newView = TouchSpotView() //TouchSportView is a custom UIView
newView.bounds = CGRect(x: 0, y: 0, width: 1, height: 1)
newView.center = touch.location(in: self)
// Add the view and animate it to a new size.
addSubview(newView)
UIView.animate(withDuration: 0.2) {
newView.bounds.size = CGSize(width: 100, height: 100)
}
// Save the views internally
touchViews[touch] = newView //touchViews is a dict, i.e. touchView[UITouch:TouchSportView]
}
func removeViewForTouch (touch : UITouch ) {
if let view = touchViews[touch] {
view.removeFromSuperview()
touchViews.removeValue(forKey: touch)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
removeViewForTouch(touch: touch)
}
}
有关完整代码,请参阅苹果文档的附件 URL。
非常感谢您的慷慨帮助!!!!
最佳答案
尝试了以下代码(从您提供的 link 复制粘贴),它在实际设备和模拟器上都能完美运行:
import UIKit
class ViewController: UIViewController {
override func loadView() {
view = TouchableView()
view.backgroundColor = .white
}
}
class TouchableView: UIView {
var touchViews = [UITouch:TouchSpotView]()
override init(frame: CGRect) {
super.init(frame: frame)
isMultipleTouchEnabled = true
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
isMultipleTouchEnabled = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
createViewForTouch(touch: touch)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let view = viewForTouch(touch: touch)
// Move the view to the new location.
let newLocation = touch.location(in: self)
view?.center = newLocation
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
removeViewForTouch(touch: touch)
}
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
removeViewForTouch(touch: touch)
}
}
func createViewForTouch( touch : UITouch ) {
let newView = TouchSpotView()
newView.bounds = CGRect(x: 0, y: 0, width: 1, height: 1)
newView.center = touch.location(in: self)
// Add the view and animate it to a new size.
addSubview(newView)
UIView.animate(withDuration: 0.2) {
newView.bounds.size = CGSize(width: 100, height: 100)
}
// Save the views internally
touchViews[touch] = newView
}
func viewForTouch (touch : UITouch) -> TouchSpotView? {
return touchViews[touch]
}
func removeViewForTouch (touch : UITouch ) {
if let view = touchViews[touch] {
view.removeFromSuperview()
touchViews.removeValue(forKey: touch)
}
}
}
class TouchSpotView : UIView {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.lightGray
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// Update the corner radius when the bounds change.
override var bounds: CGRect {
get { return super.bounds }
set(newBounds) {
super.bounds = newBounds
layer.cornerRadius = newBounds.size.width / 2.0
}
}
}
因此,如果您要用它做更多的事情,请将额外的代码添加到问题中,或者如果您从代码中删除了某些内容,请告诉我们。否则,它应该工作。
编辑
为了完整起见,我包括了一个 GitHub link一个最小的项目,对我有用。
关于ios - touchesBegin() 创建的 UIView 不会被删除,因为当用户从 iPad 上移开手指时,touchesEnded() 永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48184154/
在我的应用程序中,我有一个 UIView 并排有两个 UITableView。位于这些之上的是一个 UIView(全部在 Storyboard中完成)。 它被设置为通过附加到顶部 UIView、DnD
我在我的 UIView 中实现了以下事件处理程序: - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]
当玩家按住按钮四处移动然后按下射击按钮时,会调用 TouchesEnded,然后取消玩家的移动。这两个操作单独工作,但当它们同时被调用时则不会。 override func touchesBegan(
我正在我的 iOS 应用程序中实现 touchesBegan 和 touchesEnded,试图检测用户何时将手指放在屏幕上以及何时松开。 我遇到的问题是,一旦 touchesBegan 被调用,如果
我正在尝试让 touchesEnded 在一个手势后触发。我包含了一个代码片段,其中可以触发 touchesBegan 和 touchesEnded。我用下面的代码得到了两个奇怪的行为。当我平移时,它
很抱歉让我感到尴尬,但请问我能否获得有关如何执行此操作的完整代码: 我希望在我的游戏中每发射一颗子弹后延迟 1 秒,以防止子弹垃圾邮件。如果可能的话,不要像我在 touchesEnded 中那样为子弹
当我调用 touchesBegan() 时 override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
我有一个子类 UIView 的自定义 View 。它有一个 touchesEnded 事件,但是它没有显示在 Interface Builder 中(在事件下)。我想知道如何将该方法放入我的 View
如果我用两根手指点击屏幕太快,touchesBegan被调用,但 touchesEnded不要求其中之一。 我疯狂地用谷歌搜索,这似乎是人们至少从 2010 年以来一直存在的一个严重问题。显然它还没有
我正在使用下面的代码来尝试获取触摸的位置,但它只能在按钮之外使用。例如。如果触摸在按钮之外,它会打印控制台行,但如果不是,它就不起作用。什么是解决方案?这是代码: - (void)touche
我正在构建一个 iOS 绘图应用程序,我正在尝试实现一个撤消按钮来删除用户绘制的线条。我在变量中创建了一个图像数组: var images = [UIImage]() 每次用户在屏幕上滑动和移开手指时
在我的 Storyboard中,我有一些图像是“启用用户交互”的。在 UIViewController 中,我覆盖了 touchesEnded 函数。 override func touchesEnd
我正在使用 spritekit 制作游戏,触摸开始和触摸结束之间有明显的 100-200 毫秒延迟。 有什么办法可以加快速度吗?我需要使用 touches ended(计算用户触摸的起点和终点之间的矢
代码将返回 x1 和 y1 值,但似乎并没有通过 touchesEnded 函数运行。我的目标是创建一个矩形,从用户触摸的角开始,到用户抬起手指的地方结束。 //touch i
我有一个带有主菜单的 SpriteKit 游戏。菜单标签在 touchesEnded 上触发。这在第一次启动游戏时工作正常。 然后,第一关结束后,再次呈现主菜单。这次没有触发touchesEnded!
我有一段简单的代码,其中覆盖了 touchesBegan、Ended、Cancelled(empty block) 和 touchesMoved。如果我用鼠标单击(我在台式电脑上测试) touches
好的-所以我有一个子类 UIView 的应用程序,并依赖于 touchesBegan 和 touchesEnded。我当然指的是这些回调: - (void)touchesBegan:(NSSet *)
我正在为 iOS 开发一个应用程序,我希望用户能够在屏幕上选择一些东西,然后在选择结束时能够按住手指一秒钟,以触发其他一些东西事件。 我已经创建了很多 Action ,用于当用户通过四处滑动来标记内容
我有一个 UIView 子类,我在其中使用 touchesBegan、touchesMoved、touchesEnded 处理触摸。我注意到当我在内部开始触摸然后拖动到 UIView 外部时 touc
我有一个名为“MyView”的 UIView 子类。在ControllerA中,它有一个属性MyView *myView,并将其添加到self.view中。在myView上,有一些随机的 subvie
我是一名优秀的程序员,十分优秀!