gpt4 book ai didi

swift - 绘制 View : the drawing is not in the same position as my finger or my cursor

转载 作者:行者123 更新时间:2023-11-30 11:10:53 24 4
gpt4 key购买 nike

我创建了一个可以绘图的 View ,但是当我绘图时,该线是在光标或手指的下方创建的。

红线是我的手指或光标的移动,黑线是绘制的内容

enter image description here

   class DrawViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!
var lastPoint = CGPoint.zero
var swiped = false

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
swiped = false
if let touch = touches.first {
lastPoint = touch.location(in: self.view)
}
}

func drawLines(fromPoint:CGPoint,toPoint:CGPoint) {

UIGraphicsBeginImageContext(self.view.frame.size)
imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
let context = UIGraphicsGetCurrentContext()

context?.move(to: CGPoint(x: fromPoint.x, y: fromPoint.y))
context?.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y))

context?.setBlendMode(CGBlendMode.normal)
context?.setLineCap(CGLineCap.round)
context?.setLineWidth(5.0)
context?.setStrokeColor(UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0).cgColor)

context?.strokePath()

imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?){
swiped = true
if let touch = touches.first {
let currentPoint = touch.location(in: self.view)
drawLines(fromPoint: lastPoint, toPoint: currentPoint)
lastPoint = currentPoint
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?){ if !swiped { drawLines(fromPoint: lastPoint, toPoint: lastPoint) } }

override func viewDidLoad() {super.viewDidLoad()}
override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()}

最佳答案

尝试更改 imageView 温度

imageView.image?.draw(in: view.bounds)

和:

context.move(to: fromPoint)
context.addLine(to: toPoint)

祝你好运

关于swift - 绘制 View : the drawing is not in the same position as my finger or my cursor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52193468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com