首页 > 解决方案 > 由于滚动视图,Touches 命令不起作用

问题描述

嗨,我是快速编程的新手。我想创建一个可以允许用户在图像上绘图并且绘图功能有效的应用程序但是当我添加滚动视图时它似乎阻止了应该用于 imageView 的触摸命令有什么我可以做的吗允许用户在图像上绘制并保持滚动视图?scrollView 在那里,以便用户可以放大和缩小图像

let strokeColor:UIColor = UIColor.red

let lineWidth:CGFloat = 1.0

var path = UIBezierPath()
var shapeLayer = CAShapeLayer()
var croppedImage = UIImage()

@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()
    imageView.image = img
    self.scrollView.minimumZoomScale = 1.0
    self.scrollView.maximumZoomScale = 6.0

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return self.imageView
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first as UITouch?{
        let touchPoint = touch.location(in: self.imageView)
        path.move(to: touchPoint)
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first as UITouch?{
        let touchPoint = touch.location(in: self.imageView)
        path.addLine(to: touchPoint)
        addNewPathToImage()
    }
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first as UITouch?{
        let touchPoint = touch.location(in: self.imageView)
        path.close()
    }
}

func addNewPathToImage(){
    shapeLayer.path = path.cgPath
    shapeLayer.strokeColor = strokeColor.cgColor
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.lineWidth = lineWidth
    imageView.layer.addSublayer(shapeLayer)
}

标签: swift

解决方案


推荐阅读