首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带虚线的UIView

带虚线的UIView
EN

Stack Overflow用户
提问于 2012-08-23 20:45:53
回答 9查看 51.4K关注 0票数 33

我所拥有的:

要创建这行代码,我基本上有一个UIView,并执行以下操作:

void setLayerToLineFromAToB(CALayer *layer, CGPoint a, CGPoint b, CGFloat lineWidth)
{
    CGPoint center = { 0.5 * (a.x + b.x), 0.5 * (a.y + b.y) };
    CGFloat length = sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    CGFloat angle = atan2(a.y - b.y, a.x - b.x);

    layer.position = center;
    layer.bounds = (CGRect) { {0, 0}, { length + lineWidth, lineWidth } };
    layer.transform = CATransform3DMakeRotation(angle, 0, 0, 1);
}

注意:这段代码是在stackoverflow上找到的,所以如果有人能给我参考,我将不胜感激。

我想要的:

好的,我需要做的“唯一”事情就是在UIView上创建这个模式。我知道我可以使用Quartz2D (可以在here中找到一种简单的方法)来做到这一点。但我想通过操作CALayer来实现,而不是转到draw方法。为什么?由于我在UIView上进行的转换,我无法使用draw方法正确绘制。

编辑1:

只是为了说明我的问题:

通常你得到的是UIView,然后你基本上只需要在里面画一些东西(在本例中是一条简单的线)。我找到的摆脱“灰色”区域的解决方案是,不画什么,只需要改变UIView本身。它工作得很好,如果你想要一个完全填充的行,当你想要一个虚线的时候,问题就来了。

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2012-08-24 16:39:27

注释:普林斯的代码确实帮了我很大的忙,所以我会给他+10的提示。但最后,我添加了我自己的代码。我还将为它添加一些上下文,以便它对将来的读者有用

最终的代码是这样的:

-(void)updateLine{

      // Important, otherwise we will be adding multiple sub layers
      if ([[[self layer] sublayers] objectAtIndex:0])
        {
            self.layer.sublayers = nil;
        }

        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, beginPoint.center.x, beginPoint.center.y);
        CGPathAddLineToPoint(path, NULL, endPoint.center.x, endPoint.center.y);

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

        [[self layer] addSublayer:shapeLayer];
}

在我的例子中,用户可以使用KVO移动beginPoint和endPoint。所以当其中一个移动的时候:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqual:@"position"])
    {
        [self updateLine];
    }
}

我确实用了很多普林斯的代码。我尝试了draw:方法,它在虚线之间添加了一条细线(有点奇怪……)我也在initWithFrame:上试过。他的代码本身,在没有任何修改的情况下,会在控制台上给出这种错误:

<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetLineWidth: invalid context 0x0
<Error>: CGContextSetLineJoin: invalid context 0x0
<Error>: CGContextSetLineCap: invalid context 0x0
<Error>: CGContextSetMiterLimit: invalid context 0x0
<Error>: CGContextSetFlatness: invalid context 0x0
<Error>: CGContextAddPath: invalid context 0x0
<Error>: CGContextDrawPath: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
票数 24
EN

Stack Overflow用户

发布于 2012-08-23 20:50:42

Check UIBezierPath setLineDash:count:phase: method

- (void)setLineDash:(const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase` method. 

这使您可以绘制虚线。

  1. 首先添加一个CAShapeLayer。将其作为子层添加到您的UIView。它有一个path属性。
  2. 现在为UIBezierPath创建一个对象。使用setLineDash.

绘制直线

为例:

 UIBezierPath *path = [UIBezierPath bezierPath];
 //draw a line
 [path moveToPoint:yourStartPoint]; //add yourStartPoint here
 [path addLineToPoint:yourEndPoint];// add yourEndPoint here
 [path stroke];

 CGFloat dashPattern[] = {2.0f,6.0f,4.0f,2.0f}; //make your pattern here
 [path setLineDash:dashPattern count:4 phase:3];

 UIColor *fill = [UIColor blueColor];
 shapelayer.strokeStart = 0.0;
 shapelayer.strokeColor = fill.CGColor;
 shapelayer.lineWidth = 5.0;
 shapelayer.lineJoin = kCALineJoinMiter;
 shapelayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:7], nil];
 shapelayer.lineDashPhase = 3.0f;
 shapelayer.path = path.CGPath;

注意:此答案提供了提示,以便您可以根据自己的要求即兴发挥。

票数 43
EN

Stack Overflow用户

发布于 2018-01-17 14:56:58

Swift4·Xcode 9中的虚线

使用lineDashPattern创建CAShapeLayer

extension UIView {

    func addDashedBorder() {
        //Create a CAShapeLayer
        let shapeLayer = CAShapeLayer()
        shapeLayer.strokeColor = UIColor.red.cgColor
        shapeLayer.lineWidth = 2
        // passing an array with the values [2,3] sets a dash pattern that alternates between a 2-user-space-unit-long painted segment and a 3-user-space-unit-long unpainted segment
        shapeLayer.lineDashPattern = [2,3]

        let path = CGMutablePath()
        path.addLines(between: [CGPoint(x: 0, y: 0),
                                CGPoint(x: self.frame.width, y: 0)])
        shapeLayer.path = path
        layer.addSublayer(shapeLayer)
    }
}

使用:

dashView.addDashedBorder()

输出:

票数 29
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12091916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档