gpt4 book ai didi

ios - Swift:无法触摸 scrollView 上的 Canvas View

转载 作者:行者123 更新时间:2023-11-28 13:46:37 24 4
gpt4 key购买 nike

我正在尝试在我的应用中构建图像编辑功能。我跟着这个tutorial启用缩放功能,这 tutorial对于绘图部分。

我正在实现绘图部分并意识到 canvas View 在覆盖到 scrollView 上时未检测到触摸。当前的层次结构是这样的:

scrollView -> imageView -> canvas

到目前为止的实现:

//At the viewController that implements the Canvas UIView
var imageView: UIImageView!
var canvas = Canvas()

override func viewDidLoad() {
super.viewDidLoad()

let image = UIImage(named: "testImage")
imageView = UIImageView(image: image)

setupCanvas()
}

func setupCanvas() {
imageView.addSubview(canvas)
canvas.backgroundColor = UIColor(white: 1, alpha: 0.4)
canvas.frame = CGRect(x: 0, y: 0, width: 3000, height: 3000)
canvas.isUserInteractionEnabled = true
}

//At the Canvas class
class Canvas: UIView {

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: self) else {return}

print("Moved", point) //Breakpoint here not called
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: self) else {return}
print("Began", point) //Breakpoint here not called

}
}

此时,触摸似乎与 scrollView 交互。有没有一种方法可以检测 canvas View 上的 touches

最佳答案

好的,基本错误。应该是

scrollView.addSubview(canvas)

相反。

关于ios - Swift:无法触摸 scrollView 上的 Canvas View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55323951/

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