gpt4 book ai didi

swift - 调用touchesEnded时移除 View

转载 作者:行者123 更新时间:2023-11-30 10:35:27 25 4
gpt4 key购买 nike

当用户触摸并按住手指在屏幕上时,我使用touchesBegan在屏幕上添加 View

参见下面的示例

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: view)
let dot = CustomTouch(frame: CGRect(x: location.x, y: location.y, width: 80, height: 80))
dot.backgroundColor = randomColors[0]
self.view.addSubview(dot)
}
}

我想检测用户何时从屏幕上抬起手指,然后删除在该位置添加的 View 。我想要一些关于如何实现这一目标的想法

最佳答案

为添加的 View 添加一个属性,而不是动态创建,然后删除(如果在 TouchsEnded 上存在的话):

var dot: CustomTouch?

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: view)
dot = CustomTouch(frame: CGRect(x: location.x, y: location.y, width: 80, height: 80))
dot.backgroundColor = randomColors[0]
self.view.addSubview(dot)
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
dot?.removeFromSuperview()
}

关于swift - 调用touchesEnded时移除 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58158071/

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