У меня есть приложение для рисования с использованием Core Graphics. Все работает нормально. Я умею рисовать. Я добавил кнопку «отменить», чтобы отменить линию. Он работает, но ...Добавление массива CGPoints в массив: Array <Array <CGPoint>>?
Проблема: каждый раз, когда нажимается кнопка «Отменить», она удаляется последовательно за строкой. Я пытаюсь добавить массив CGPath к массиву, который будет содержать весь рисунок. например Если я рисую линию от левого края экрана до правого края экрана, когда я нажимаю «UNDO», он должен удалить этот «путь» (массив CGPoints). В настоящее время он выполняет линию за строкой, которая принимает миллион вызовов «UNDO», чтобы удалить тот же самый путь.
Возможное решение: Я думаю, что лучший способ - собрать массив CGPoints из touchhesBegans через touchesEnded, а затем добавить его в массив, содержащий массив 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
}
}
Я попробовал несколько разных способов t o добавить массив строк в массив allLines, но без успеха.
Как я могу добавить мои строки сделать это:
array<array<CGPoint>>
Я также создал приложение для рисования. Вы можете посмотреть здесь, вы можете найти что-то полезное ... https://github.com/Gerst20051/Swift/tree/master/HarmonyCanvas/HarmonyCanvas –