swift - 由于滚动视图,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)
}
解决方案
推荐阅读
- unity3d - unity 清新 场景太暗
- django - 过滤外键下拉字段 Django
- date - Pyspark:如何在日期上创建窗口
- angular - 订阅 rxjs 中的超时
- makefile - 无法使用 Makefile 链接到 SFML 库,即使它作为一个衬垫工作
- javascript - 如何在等待 MVC 调用返回时为新选项卡显示加载图像或进度条?
- javascript - 根据日期时间对 ag-grid 列进行排序
- xml - 在 1 个 XSLT 文件中使用 if 语句来转换 2 种不同类型的 XML 文件
- utf-8 - 从 pc 到 mac 的逻辑符号不同
- javascript - 使用 JavaScript 返回 HTML 输入