gpt4 book ai didi

ios - 将 CGPoints 数组附加到数组 : Array>?

转载 作者:行者123 更新时间:2023-11-28 07:02:00 27 4
gpt4 key购买 nike

我有一个使用 Core Graphics 的绘图应用程序。一切正常。我会画画。我添加了一个“撤消”按钮来撤消该行。它有效,但是......

问题:每次按下“撤消”按钮时,它都会逐行删除。我正在尝试将 CGPath 数组附加到将保存整个绘图的数组。例如如果我从屏幕的左边缘到屏幕的右边缘画一条线,当我按下“UNDO”时,它应该删除这个“路径”(CGPoints 数组)。它目前正在逐行进行,这需要像一百万次“撤消”调用才能删除相同的绘制路径。

可能的解决方案:我认为最好的方法是从 touchesBegans 到 touchesEnded 收集 CGPoints 数组,然后将其附加到包含 CGPoints 数组的数组。

 array<array<CGPoint>> //Using something like this

MainViewController:调用“UNDO”操作

 //calls the "undo" in UIView class
@IBAction func undoButton(sender: AnyObject) {
var theDrawView = drawView as DrawView
theDrawView.undoLastPath()
}

自定义 UIView 类:

var lines: [Line] = []

var lastPoint: CGPoint!
var drawColor = UIColor.redColor()
var allLines = Array<Array<CGPoint>>()//Will store arrays of CGPoint Arrays


required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch = touches.first as! UITouch
lastPoint = touch.locationInView(self)
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch = touches.first as! UITouch
var newPoint = touch.locationInView(self)

lines.append(Line(start: lastPoint, end: newPoint, color: drawColor))
lastPoint = newPoint
self.setNeedsDisplay()

}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch = touches.first as! UITouch
var newPoint = touch.previousLocationInView(self)
//ERROR: lines are not appending!
allLines.append(lines(start: lastPoint, end: newPoint, color: drawColor))

touchesMoved(touches, withEvent: event)
}

override func drawRect(rect: CGRect) {
var context = UIGraphicsGetCurrentContext()
CGContextBeginPath(context)
CGContextSetLineWidth(context, 10.0)
CGContextSetLineCap(context, kCGLineCapRound)

//Currently this works: Will add the new allLines (Array)
for line in lines {

CGContextMoveToPoint(context, line.start.x, line.start.y)
CGContextAddLineToPoint(context, line.end.x, line.end.y)

//CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0)
CGContextSetStrokeColorWithColor(context, line.color.CGColor)
CGContextStrokePath(context)
}
}
func eraseAll(){
lines.removeAll(keepCapacity: false)
self.setNeedsDisplay()
}

为我的线路定制 Swift 类:

class Line {
var start: CGPoint
var end: CGPoint
var color: UIColor
init(start _start: CGPoint, end _end: CGPoint, color: UIColor) {
start = _start
end = _end
color = _color
}
}

我尝试了多种不同的方法将 lines 数组附加到 allLines 数组但没有成功。

如何将我的线条附加到此:

 array<array<CGPoint>>

最佳答案

我相信你应该让你的 allLines 成为 Line 数组的数组:

var allLines = [[Line]]()

然后你的touchesEnded变成:

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch = touches.first as! UITouch
var newPoint = touch.previousLocationInView(self)
lines.append(Line(start: lastPoint, end: newPoint, color: drawColor))
allLines.append(lines)

// Prep lines to hold the next line
lines = []
}

在你的drawRect中:

for lines in allLines {
for line in lines {

CGContextMoveToPoint(context, line.start.x, line.start.y)
CGContextAddLineToPoint(context, line.end.x, line.end.y)

//CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0)
CGContextSetStrokeColorWithColor(context, line.color.CGColor)
CGContextStrokePath(context)
}
}

undoLastPath 很简单:

func undoLastPath() {
if count(allLines) > 0 {
allLines.removeLast()
self.setNeedsDisplay()
}
}

关于ios - 将 CGPoints 数组附加到数组 : Array<Array<CGPoint>>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31822581/

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