我正在做这些功能:
我正在通过Paint Bucket进行图像着色。
zoomin用scrollview缩小图像。
合并这两个功能会使我的设备挂起一段时间。
请检查以下代码工作并协助我。
- 在imageView上点击func
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool{
let touchView = gestureRecognizer.view
if touchView == self.imgViewMy{
let point : CGPoint = touch.location(in: imgViewMy)
touchedPoint = point
self.imgViewMy.image = self.imgDup
// Doing asysn coloring in image to avoid device hang up.
DispatchQueue.main.async( execute: {
print("touch is inside , \(self.touchedPoint!)")
// Getting scaled coordinates
let aNewCor = self.getScaledPoint(self.touchedPoint!)
// Calling Paint Bucket function to change image color.
self.imgDup = self.imgDup?.pbk_imageByReplacingColorAt(Int(aNewCor.x),
Int(aNewCor.y),
withColor: self.colorSel,
tolerance: 10)
self.imgViewMy.image = self.imgDup
})
return false
}
return false
}
- 适用于图像放大&缩小
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imgViewMy
}
答案 0 :(得分:2)
尝试简化并进行一些清理:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool{
let touchView = gestureRecognizer.view
if touchView == self.imgViewMy {
let point : CGPoint = touch.location(in: imgViewMy)
touchedPoint = point
}
return true
}
func tapResponse(sender: UITapGestureRecognizer? = nil) {
let aNewCor = self.getScaledPoint(self.touchedPoint!)
self.imgViewMy.image = self.imgDup?.pbk_imageByReplacingColorAt(Int(aNewCor.x), Int(aNewCor.y), withColor: self.colorSel, tolerance: 5)
}
在设备上测试,似乎工作正常(它非常敏感)。