将 CGPoints 数组附加到数组:Array<Array<CGPoint>>?
Appending array of CGPoints to array: Array<Array<CGPoint>>?
我有一个使用 Core Graphics 的绘图应用程序。一切正常。我会画画。我添加了一个 "undo" 按钮来撤消该行。它有效,但是...
问题:每次按下 "undo" 按钮时,它都会逐行删除。我正在尝试将 CGPath 数组附加到将保存整个绘图的数组。例如如果我从屏幕的左边缘到屏幕的右边缘画一条线,当我按下 "UNDO" 时,它应该删除这个 'path'(CGPoints 数组)。它目前正在逐行进行,这需要大约一百万次 "UNDO" 次调用才能删除相同的绘制路径。
可能的解决方案:我认为最好的方法是从 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 Class:
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 我的行:
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()
}
}
我有一个使用 Core Graphics 的绘图应用程序。一切正常。我会画画。我添加了一个 "undo" 按钮来撤消该行。它有效,但是...
问题:每次按下 "undo" 按钮时,它都会逐行删除。我正在尝试将 CGPath 数组附加到将保存整个绘图的数组。例如如果我从屏幕的左边缘到屏幕的右边缘画一条线,当我按下 "UNDO" 时,它应该删除这个 'path'(CGPoints 数组)。它目前正在逐行进行,这需要大约一百万次 "UNDO" 次调用才能删除相同的绘制路径。
可能的解决方案:我认为最好的方法是从 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 Class:
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 我的行:
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()
}
}