画线时触摸结束时线不一致
line disagreeing when touch end when drawing line
我的 swift 代码目标是在图像视图上画线。现在,当您从图像中拿起手时,绘图就会消失。当您的手被按下时,它会起作用,当您的手或点被释放时,什么都不会保存。我不知道我的代码中缺少什么来解决这个问题,它可能只是一行。
import UIKit
class ViewController: UIViewController {
var statPoint = CGPoint.zero
var swipe = false
var pic = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
pic.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pic)
pic.backgroundColor = .brown
view.backgroundColor = .cyan
pic.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return}
swipe = false
statPoint = touch.location(in: view)
}
func drawLine(from fromPoint: CGPoint, to toPoint : CGPoint) {
UIGraphicsBeginImageContext( view.frame.size)
guard let context = UIGraphicsGetCurrentContext() else {
return
}
pic.image?.draw(in: view.bounds)
context.move(to: fromPoint)
context.addLine(to: toPoint)
context.setLineCap(.round)
context.setLineWidth(5)
context.setBlendMode(.normal)
context.setStrokeColor(UIColor.black.cgColor)
context.strokePath()
pic.image = UIGraphicsGetImageFromCurrentImageContext()
pic.alpha = 1
UIGraphicsEndImageContext()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return
}
swipe = true
let currentPoint = touch.location(in: view)
drawLine(from: statPoint, to: currentPoint)
statPoint = currentPoint
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if !swipe {
drawLine(from: statPoint, to: statPoint)
}
UIGraphicsBeginImageContext(pic.frame.size)
pic.image?.draw(in: view.bounds, blendMode: .normal, alpha: 1)
pic.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
}
我已经检查了你的代码。我认为问题出在您在 touchedEnded.
中使用的 frame
用下面的代码替换你的touchedEnded
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if !swipe {
drawLine(from: statPoint, to: statPoint)
}
UIGraphicsBeginImageContext(view.frame.size)
pic.image?.draw(in: view.bounds, blendMode: .normal, alpha: 1)
pic.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
我的 swift 代码目标是在图像视图上画线。现在,当您从图像中拿起手时,绘图就会消失。当您的手被按下时,它会起作用,当您的手或点被释放时,什么都不会保存。我不知道我的代码中缺少什么来解决这个问题,它可能只是一行。
import UIKit
class ViewController: UIViewController {
var statPoint = CGPoint.zero
var swipe = false
var pic = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
pic.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pic)
pic.backgroundColor = .brown
view.backgroundColor = .cyan
pic.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return}
swipe = false
statPoint = touch.location(in: view)
}
func drawLine(from fromPoint: CGPoint, to toPoint : CGPoint) {
UIGraphicsBeginImageContext( view.frame.size)
guard let context = UIGraphicsGetCurrentContext() else {
return
}
pic.image?.draw(in: view.bounds)
context.move(to: fromPoint)
context.addLine(to: toPoint)
context.setLineCap(.round)
context.setLineWidth(5)
context.setBlendMode(.normal)
context.setStrokeColor(UIColor.black.cgColor)
context.strokePath()
pic.image = UIGraphicsGetImageFromCurrentImageContext()
pic.alpha = 1
UIGraphicsEndImageContext()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return
}
swipe = true
let currentPoint = touch.location(in: view)
drawLine(from: statPoint, to: currentPoint)
statPoint = currentPoint
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if !swipe {
drawLine(from: statPoint, to: statPoint)
}
UIGraphicsBeginImageContext(pic.frame.size)
pic.image?.draw(in: view.bounds, blendMode: .normal, alpha: 1)
pic.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
}
我已经检查了你的代码。我认为问题出在您在 touchedEnded.
中使用的 frame用下面的代码替换你的touchedEnded
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if !swipe {
drawLine(from: statPoint, to: statPoint)
}
UIGraphicsBeginImageContext(view.frame.size)
pic.image?.draw(in: view.bounds, blendMode: .normal, alpha: 1)
pic.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}