2012-10-03 70 views
19

我能畫一個虛線框,使用下面的代碼行:繪圖虛線使用的CALayer

CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 
CGRect shapeRect = CGRectMake(0.0f, 0.0f, 200.0f, 100.0f); 
[shapeLayer setBounds:shapeRect]; 
[shapeLayer setPosition:CGPointMake(self.coreImageView_.frameX, self.coreImageView_.frameBottom - self.coreImageView_.frameHeight/2)]; 
[shapeLayer setFillColor:[[UIColor clearColor] CGColor]]; 
[shapeLayer setStrokeColor:[[UIColor whiteColor] CGColor]]; 
[shapeLayer setLineWidth:2.0f]; 
[shapeLayer setLineJoin:kCALineJoinRound]; 
[shapeLayer setLineDashPattern: 
[NSArray arrayWithObjects:[NSNumber numberWithInt:5], 
[NSNumber numberWithInt:5], 
    nil]]; 

現在,如果我想只吸取X點虛線到B點,我應該怎麼修改這個代碼?

+0

你是什麼意思由點X和點B?它們是矩形上的點,還是屏幕上任何地方只有2點? –

+0

可能的重複[here](http://stackoverflow.com/questions/12091916/uiview-with-a-dashed-line) –

+0

http://stackoverflow.com/questions/12091916/uiview-with-a-dashed- line/12092002#12092002 –

回答

55

行由第一路徑移動到該行的起點,然後加入線段的點繪製:

CGContextBeginPath(context); 
CGContextMoveToPoint(context, 10.5f, 10.5f); 
CGContextAddLineToPoint(context, 20.5f, 20.5f); 
CGContextClosePath(context); 
CGContextDrawPath(context, kCGPathFillStroke); 

繪製虛線,您需要使用CAShapeLayer

CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 
[shapeLayer setBounds:self.bounds]; 
[shapeLayer setPosition:self.center]; 
[shapeLayer setFillColor:[[UIColor clearColor] CGColor]]; 
[shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]]; 
[shapeLayer setLineWidth:3.0f]; 
[shapeLayer setLineJoin:kCALineJoinRound]; 
[shapeLayer setLineDashPattern: 
[NSArray arrayWithObjects:[NSNumber numberWithInt:10], 
    [NSNumber numberWithInt:5],nil]]; 

// Setup the path 
CGMutablePathRef path = CGPathCreateMutable(); 
CGPathMoveToPoint(path, NULL, 10, 10); 
CGPathAddLineToPoint(path, NULL, 100,100); 

[shapeLayer setPath:path]; 
CGPathRelease(path); 

[[self layer] addSublayer:shapeLayer]; 
+0

我在牆上敲了幾個小時,試圖用CGContext做到這一點。非常感謝! – user1244109

0

雨燕2.2

在這裏救人時下降這個..

extension UIView { 
    func addDashedLine(color: UIColor = UIColor.lightGrayColor()) { 
     layer.sublayers?.filter({ $0.name == "DashedTopLine" }).map({ $0.removeFromSuperlayer() }) 
     self.backgroundColor = UIColor.clearColor() 
     let cgColor = color.CGColor 

     let shapeLayer: CAShapeLayer = CAShapeLayer() 
     let frameSize = self.frame.size 
     let shapeRect = CGRect(x: 0, y: 0, width: frameSize.width, height: frameSize.height) 

     shapeLayer.name = "DashedTopLine" 
     shapeLayer.bounds = shapeRect 
     shapeLayer.position = CGPoint(x: frameSize.width/2, y: frameSize.height/2) 
     shapeLayer.fillColor = UIColor.clearColor().CGColor 
     shapeLayer.strokeColor = cgColor 
     shapeLayer.lineWidth = 1 
     shapeLayer.lineJoin = kCALineJoinRound 
     shapeLayer.lineDashPattern = [4, 4] 

     let path: CGMutablePathRef = CGPathCreateMutable() 
     CGPathMoveToPoint(path, nil, 0, 0) 
     CGPathAddLineToPoint(path, nil, self.frame.width, 0) 
     shapeLayer.path = path 

     self.layer.addSublayer(shapeLayer) 
    } 
} 
1

斯威夫特,更緊湊:

func addDashedLine(fromPoint start: CGPoint, toPoint end:CGPoint) { 
    let line = CAShapeLayer() 
    let linePath = UIBezierPath() 
    linePath.moveToPoint(start) 
    linePath.addLineToPoint(end) 
    line.path = linePath.CGPath 
    line.strokeColor = UIColor.redColor().CGColor 
    line.lineWidth = 1 
    line.lineJoin = kCALineJoinRound 
    line.lineDashPattern = [4, 4] 
    self.layer.addSublayer(line) 
} 
2

試試這個代碼,它爲我工作,

Swift 3.0

extension UIView { 
    func addDashedLine(strokeColor: UIColor, lineWidth: CGFloat) { 

     backgroundColor = .clear 

     let shapeLayer = CAShapeLayer() 
     shapeLayer.name = "DashedTopLine" 
     shapeLayer.bounds = bounds 
     shapeLayer.position = CGPoint(x: frame.width/2, y: frame.height/2) 
     shapeLayer.fillColor = UIColor.clear.cgColor 
     shapeLayer.strokeColor = strokeColor.cgColor 
     shapeLayer.lineWidth = lineWidth 
     shapeLayer.lineJoin = kCALineJoinRound 
     shapeLayer.lineDashPattern = [4, 4] 

     let path = CGMutablePath() 
     path.move(to: CGPoint.zero) 
     path.addLine(to: CGPoint(x: frame.width, y: 0)) 
     shapeLayer.path = path 

     layer.addSublayer(shapeLayer) 
    } 
} 
0

得到它在目標C的工作與簡化的代碼如下

//Dashed line for road 
    CAShapeLayer *dashedLine = [CAShapeLayer layer]; 
    [dashedLine setFrame:CGRectMake(0, 342, 100 , 100)]; 

    // Setup the path 
    CGMutablePathRef thePath = CGPathCreateMutable(); 
    CGPathMoveToPoint(thePath, NULL, 0, 10); 
    CGPathAddLineToPoint(thePath, NULL, screenSize.width,10); 
    dashedLine.path = thePath; 
    CGPathRelease(thePath); 

    [dashedLine setLineDashPattern: [NSArray arrayWithObjects:[NSNumber numberWithFloat:15], nil]]; 
    dashedLine.lineWidth = 1.0f; 
    dashedLine.strokeColor = [[UIColor redcolor] CGColor]]; 

    [self.view.layer addSublayer:dashedLine]; 
0
CAShapeLayer *shaplayer = [CAShapeLayer layer]; 
    shaplayer.frame = CGRectMake(100, 100, 100, 100); 
    [self.view.layer addSublayer:shaplayer]; 
    UIBezierPath *uipath = [UIBezierPath bezierPath]; 
    [uipath moveToPoint:CGPointMake(50, 200)]; 
    [uipath addLineToPoint:CGPointMake(250, 200)]; 
    shaplayer.path = uipath.CGPath; 
    shaplayer.strokeColor = [UIColor redColor].CGColor; 
    shaplayer.lineDashPattern = @[@4]; 
+0

感謝您使用此代碼段,該代碼段可能會提供一些即時幫助。通過說明爲什麼這是一個很好的解決問題的辦法,一個適當的解釋會[大大提高](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)其教育價值,並會使其對未來具有相似但不相同問題的讀者更有用。請編輯您的答案以添加解釋,並指出適用的限制和假設。 – basvk

0

下面是代碼段來繪製虛線的UIView(Xamarin的iOS)

注: separatorView是我的UIView,我需要顯示爲虛線

public void ShowDottedLine() 
{ 
     var dashedLayer = new CAShapeLayer(); 
     var frameSize = separatorView.Frame.Size; 
     var shapeRect = new CGRect(0, 0, frameSize.Width, frameSize.Height); 
     dashedLayer.Bounds = shapeRect; 
     dashedLayer.Position = new CGPoint(frameSize.Width/2, frameSize.Height/2); 
     dashedLayer.FillColor = UIColor.Clear.CGColor; 
     dashedLayer.StrokeColor = ColorUtils.ColorWithHex(ColorConstants.DarkBlue).CGColor; 
     dashedLayer.LineWidth = 2; 
     dashedLayer.LineJoin = CAShapeLayer.JoinRound; 
     NSNumber[] patternArray = {5,5}; 
     dashedLayer.LineDashPattern = Array; 
     var path = new CGPath(); 
     path.MoveToPoint(CGPoint.Empty); 
     path.AddLineToPoint(new CGPoint(frameSize.Width, 0)); 
     dashedLayer.Path = path; 
     separatorView.Layer.AddSublayer(dashedLayer); 
}