gpt4 book ai didi

swift - Touchesmoved - 单次拖动更新多个元素

转载 作者:行者123 更新时间:2023-11-28 06:35:03 25 4
gpt4 key购买 nike

我正在尝试通过单击并拖动元素来更新多个图像。我已经实现了 touchesbegan、touchesmoved 和 touchesended,但我不知道如何使 touchesmoved 影响多个图像。

我一直在网上搜索,但没能找到这方面的任何指南。如果您能为我指明正确的方向,或为我提供一些基本建议,将不胜感激。

以下是示例图片:

编辑:以下是应该可能的示例图像:

What it should look like.

我希望能够通过相同的按压以相同的方式影响其他字母,改变它们的图片。这些图片是临时图片。

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("touches began:\(touches)")
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.image = UIImage(named: "slot")!
print("touches moved:\(touches)")
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.image = UIImage(named: "tile")!
print("touches ended")
}

最佳答案

如果我没理解错的话,应该是这样的:

class ChangableView: UIView {
private var touchedImageView: UIImageView? {
didSet {
if oldValue == touchedImageView { return }
if let oldValue = oldValue {
oldValue.image = UIImage(named: "tile")!
}
if let newValue = touchedImageView {
newValue.image = UIImage(named: "slot")!
}
}
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard let touch = touches.first else { return }
updateTouchedImageViewWithTouch(touch, event: event)
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard let touch = touches.first else { return }
updateTouchedImageViewWithTouch(touch, event: event)
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
touchedImageView = nil
}
}

private extension ChangableView {
func updateTouchedImageViewWithTouch(touch: UITouch, event: UIEvent?) {
let touchPoint = touch.locationInView(self)
let touchedView = self.hitTest(touchPoint, withEvent: event)
touchedImageView = touchedView as? UIImageView
}
}

此外,您的 UIViewController 应该具有 ChangableView 的子类 View ,并且不要忘记将所有 UIImageView 的 userInteractionEnabled 属性设置为 YES。

关于swift - Touchesmoved - 单次拖动更新多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39315020/

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