我有一个使用Core Graphics的绘图应用程序。一切正常。我能够画画。我添加了一个"撤消"按钮撤消该行。它有效,但是......
问题:每次"撤消"按下按钮逐行删除。我试图将CGPath数组附加到一个将保存整个绘图的数组。例如如果我从屏幕的左边缘到屏幕的右边缘画一条线,当我按下" UNDO"时,它应该删除这条'路径'(CGPoints数组) 。它目前正在逐行进行,这需要一百万" UNDO"调用删除相同的drawpath。
可能的解决方案:我认为最好的方法是从touchesBegans通过touchesEnded收集一系列CGPoints,然后将其附加到一个包含CGPoints数组的数组中。
array<array<CGPoint>> //Using something like this
MainViewController:调用&#34; UNDO&#34;操作
//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>>
答案 0 :(得分:1)
我相信你应该让你的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()
}
}