2016-01-11 2 views
2

Нижеприведенный класс присоединяется к UIView и рисует линии, перемещая палец по экрану. Однако при перемещении пальца очень быстро слева направо, перемещаясь сверху вниз, рисунок временно показывает острые острые края при изменении направления. Это происходит как на устройстве, так и на симуляторе.Линии рисования артефактов в Swift Xcode

Что вызывает эта проблема и как этот артефакт может быть устранен в коде, так что при быстром изменении направления движения наблюдаются только гладкие закругленные, а не острые края?


enter image description here


class drawLine: UIView 
{ 
var comittedSegments: Int = 0 
var points = [CGPoint]() 
var committedPath = UIBezierPath() 

var drawPath = UIBezierPath() 
var incrementalImage: UIImage? 

var strokeColor:UIColor? 

override func drawRect(rect: CGRect) { 
    autoreleasepool { 
     incrementalImage?.drawInRect(rect) 
     strokeColor = UIColor.darkGrayColor() 
     strokeColor?.setStroke() 
     drawPath.lineWidth = 20 
     drawPath.lineCapStyle = CGLineCap.Round 
     drawPath.stroke() 
    } 
} 

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let touch: AnyObject? = touches.first 

    comittedSegments = 0 
    committedPath.removeAllPoints() 
    points.removeAll() 

    points.append(touch!.locationInView(self)) 
} 

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let touch: AnyObject? = touches.first 
    let point = touch!.locationInView(self) 

    points.append(point) 

    if points.count == 5 
    { 
     points[3] = CGPointMake((points[2].x + points[4].x)/2.0, (points[2].y + points[4].y)/2.0) 

     committedPath.moveToPoint(points[0]) 
     committedPath.addCurveToPoint(points[3], controlPoint1: points[1], controlPoint2: points[2]) 
     comittedSegments = comittedSegments + 1 

      self.setNeedsDisplay() 

     points[0] = points[3] 
     points[1] = points[4] 

     points.removeRange(2...4) 

     drawPath = committedPath 
    } 
    else if points.count > 1 
    { 

     drawPath = committedPath.copy() as! UIBezierPath 

     drawPath.CGPath = committedPath.CGPath 

     drawPath.moveToPoint(points[0]) 
     for point in points[1..<points.count] { 
      drawPath.addLineToPoint(point) 
     } 

     self.setNeedsDisplay() 
    } 
} 

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    self.drawBitmap() 
    self.setNeedsDisplay() 

    committedPath.removeAllPoints() 
    points.removeAll() 
} 

override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) { 
    self.touchesEnded(touches!, withEvent: event) 
} 

func drawBitmap() { 
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, true, 0.0) 
    strokeColor?.setStroke() 
    if(incrementalImage == nil) { 
     let rectPath:UIBezierPath = UIBezierPath(rect: self.bounds) 
     UIColor.whiteColor().setFill() 
     rectPath.fill() 
    } 

    incrementalImage?.drawAtPoint(CGPointZero) 
    committedPath.stroke() 
    incrementalImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
} 
} 
+0

Возможно, вызванный магией 'если points.count == ** 5 ** ' – zcui93

ответ

2

В дополнение к настройке lineCapStyle установите lineJoinStyle:

drawPath.lineJoinStyle = .Round 
drawPath.lineCapStyle = .Round 
+0

Спасибо, это исправлено! – user4806509

Смежные вопросы