gpt4 book ai didi

ios - touchesBegin() 创建的 UIView 不会被删除,因为当用户从 iPad 上移开手指时,touchesEnded() 永远不会被调用

转载 作者:行者123 更新时间:2023-11-28 15:07:56 25 4
gpt4 key购买 nike

更新[11/1/2018]:

我尝试一步步测试代码,发现:

  • 从未为剩余的 UIView 调用方法touchesEnded
  • touchesCancelled 方法也是如此

我正在关注有关实现多点触控应用程序的 Apple 文档。

网址是: https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/handling_touches_in_your_view/implementing_a_multitouch_app

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com