gpt4 book ai didi

ios - 如何防止CGContext中风路径或绘制路径线条画向上 float /旋转回来?

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

使用 Swift 画线,我继续画的时间越长,它就会不断向上移动。或者它可能会向后旋转,就像在球上一样?这些图片显示我从一条线开始,然后当我绘制时,你可以看到它到达了正方形的顶部。太奇怪了!为什么会发生这种情况?我怎样才能阻止它?代码如下。

Drawing Start

Drawing End

var context: CGContext?
var adjustedImageViewRect: CGRect?
var adjustedImageView: UIImageView?

var lastPoint = CGPoint.zero
var firstPoint = CGPoint.zero
var brushWidth: CGFloat = 10.0
var opacity: CGFloat = 1.0
var swiped = false

var undermineArray: [CGPoint] = []

@IBOutlet weak var drawLineImageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
drawLineImageView.image = UIImage(named: "layer")
}

override func viewDidAppear(_ animated: Bool) {
let rect = AVMakeRect(aspectRatio: drawLineImageView.image!.size, insideRect: drawLineImageView.bounds)
adjustedImageViewRect = rect

adjustedImageView = UIImageView(frame: rect)
self.view.addSubview(adjustedImageView!)
self.view.bringSubview(toFront: adjustedImageView!)
}

func drawLine(from fromPoint: CGPoint, to toPoint: CGPoint) {

if let adjImgView = adjustedImageView {
if let adjImgViewRect = adjustedImageViewRect {

UIGraphicsBeginImageContextWithOptions(adjImgViewRect.size, false, 0)

adjImgView.image?.draw(in: adjImgView.bounds)

context = UIGraphicsGetCurrentContext()
context?.move(to: fromPoint)
context?.addLine(to: toPoint)
context?.setLineCap(CGLineCap.round)
context?.setLineWidth(brushWidth)
context?.setStrokeColor(UIColor.red.cgColor)
context?.setBlendMode(CGBlendMode.normal)
context?.strokePath()

adjImgView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
}
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//clear out previous line
firstPoint = CGPoint.zero
lastPoint = CGPoint.zero
undermineArray.removeAll()

adjustedImageView?.image = UIImage()
swiped = false
if let touch = touches.first {
if let rect = adjustedImageViewRect {
if (touch.location(in: self.adjustedImageView).y < 0
|| touch.location(in: self.adjustedImageView).y > rect.height) {
adjustedImageView?.image = UIImage()
return
}
}
firstPoint = touch.location(in: self.adjustedImageView)
lastPoint = touch.location(in: self.adjustedImageView)
undermineArray.append(firstPoint)
}
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
swiped = true
if let touch = touches.first {

var currentPoint = touch.location(in: adjustedImageView)
if let rect = adjustedImageViewRect {
if (touch.location(in: self.adjustedImageView).y < 0) {
currentPoint.y = 0
}

if (touch.location(in: self.adjustedImageView).y > rect.height) {
currentPoint.y = rect.height
}
}

undermineArray.append(currentPoint)
drawLine(from: lastPoint, to: currentPoint)
lastPoint = currentPoint
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

if let rect = adjustedImageViewRect {
if (firstPoint.y < 0 || firstPoint.y > rect.height) {
adjustedImageView?.image = UIImage()
return
}
}

if let touch = touches.first {
if let rect = adjustedImageViewRect {
if (touch.location(in: self.adjustedImageView).y < 0) {
lastPoint.y = 0
}
if (touch.location(in: self.adjustedImageView).y > rect.height) {
lastPoint.y = rect.height
}
}
}

if !swiped {
self.drawLine(from: lastPoint, to: lastPoint)
}
}

最佳答案

啊我明白了。事实证明,UIImage 的 draw() 函数绘制了图像,直到现在我才知道,“根据需要缩放它以适合。”

所以,我们将使用其他东西。我们将只使用drawAsPattern()。

改变

adjImgView.image?.draw(in: adjImgView.bounds)

adjImgView.image?.drawAsPattern(in: adjImgView.bounds)

瞧,没有发生疯狂的缩放/旋转/缩小。

关于ios - 如何防止CGContext中风路径或绘制路径线条画向上 float /旋转回来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52212626/

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