涂鸦绘制应用程序:绘制的线条在(alpha <1)时保持重叠

时间:2015-08-05 22:57:21

标签: ios swift uiview drawrect cgcontext

我的Swift涂鸦应用程序可以工作,但是当我将alpha更改为“1.0”以下时,我在屏幕上画一条线,线条保持重叠。我创建了一个自定义Line类,UIView的drawView子类。我整天都在搞乱,但似乎无法弄明白。见下文。

enter image description here

DrawView:UIView类

 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)

    //from custom Line class: sets width and opacity as well
    lines.append(Line(start: lastPoint, end: newPoint, color: drawColor, brushWidth: brushWidth, opacity: opacity))
    lastPoint = newPoint
    self.setNeedsDisplay()
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent){
    touchesMoved(touches, withEvent: event) // adds point
}

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

    for line in lines {
        CGContextSetLineWidth(context, line.brushWidth)
        CGContextSetAlpha(context, line.opacity)
        CGContextSetStrokeColorWithColor(context, line.color.CGColor)
        CGContextMoveToPoint(context, line.start.x, line.start.y)
        CGContextAddLineToPoint(context, line.end.x, line.end.y)
        CGContextStrokePath(context)
    }
}

我不知道为什么它会不断重叠。我有些人需要跟踪路径然后绘制它,或者以某种方式查看我的CGPoints是否相同,而彼此相邻则删除它或忽略它。

这只能使用一个UIView吗?

刚才我尝试使用:

var touchesEnd: Bool = false //global variable

然后在touchesEnded:

touchesEnd = true

最后在TouchesMoved:

 if touchesEnd == true {
    self.setNeedsDisplay()
 }

这是我尝试过但最近没有奏效的最新内容。

0 个答案:

没有答案