Swift:无法触摸scrollView上的画布视图

时间:2019-03-24 12:48:29

标签: ios swift

我正在尝试在我的应用程序中构建图像编辑功能。我遵循此tutorial启用缩放功能,并遵循了tutorial用于绘图部分。

我正在实现绘图部分,并意识到canvas视图在覆盖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交互。有没有一种方法可以检测touches视图上的canvas

1 个答案:

答案 0 :(得分:1)

好的,基本错误。应该是

scrollView.addSubview(canvas)

相反。