嗨,我是快速编程的新手。我想创建一个可以允许用户在图像上绘画的应用程序,并且绘画功能可以工作,但是当我添加scrollview时,似乎阻止了应该用于imageView的touch命令,我可以做任何事情允许用户在图像上绘制并保持scrollView?在那里有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)
}