【问题标题】:Appending array of CGPoints to array: Array<Array<CGPoint>>?将 CGPoints 数组附加到数组:Array<Array<CGPoint>>?
【发布时间】:2015-08-05 02:45:22
【问题描述】:

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

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

可能的解决方案:我认为最好的方法是通过 touchesEnded 从 touchesBegans 收集 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
    }
}

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

我怎样才能将我的线条画附加到这个:

 array<array<CGPoint>>

【问题讨论】:

标签: ios arrays swift cgpoint cgpath


【解决方案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()
    }
}

【讨论】:

  • 在你发帖 37 秒后我一直在搞砸。你的'drawRect:'是正确的!虽然在“TouchesEnded”中,但它没有正确绘制线条。它们本质上是破折号。我删除了“lines.append ...”并将其移至“touchesMoved”。有用! “撤消”删除整个曲线。但是,如果我画了一个波浪线,它不会在“touchesEnded”之后出现。谢谢!这很棒
猜你喜欢
  • 2020-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多