Touchesmoved - 单次拖动更新多个元素
Touchesmoved - updating multiple elements on single drag
我正在尝试通过单击并拖动元素来更新多个图像。我已经实现了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 的子类视图,并且不要忘记将所有 UIImageView 的 userInteractionEnabled 属性 设置为 YES。
我正在尝试通过单击并拖动元素来更新多个图像。我已经实现了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 的子类视图,并且不要忘记将所有 UIImageView 的 userInteractionEnabled 属性 设置为 YES。