如何为每个" subUIBezierPath"设置属性?

时间:2016-02-26 03:00:23

标签: ios objective-c uibezierpath

问题描述

我试图添加" 手势解锁"我的应用程序中的功能,但我有一个问题,如附图所示。我发现一些不必要的线也画出来了。事实上,我只需要显示连接每个"按钮"。

的行

请查看下面的图片

-> Image click here <-

以下是- (void)drawRect:(CGRect)rect

下的代码
    // Main Path
    UIBezierPath *path = [UIBezierPath bezierPath];

    // Rect path
    UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:rect];
    [path appendPath:rectPath];

    // 9 circle path
    [self.subviews enumerateObjectsUsingBlock:^(PPSingleCircle * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:obj.frame];
        [path appendPath:circlePath];

    }];

    // clip the path
    path.usesEvenOddFillRule = YES;
    [path addClip];

    // add line to each select Views
    for (int i = 0; i < [self.selectViews count]; i++) {
        PPSingleCircle *singleView = self.selectViews[i];

        if (i == 0) {
            [path moveToPoint:singleView.center];
        }else {
            [path addLineToPoint:singleView.center];
        }
    }

    // add line to current point
    [path addLineToPoint:self.currentPoint];

    // set display style
    [kLineColor setStroke];
    path.lineWidth = kLineWidth;
    path.lineJoinStyle = kCGLineJoinRound;
    path.lineCapStyle  = kCGLineCapRound;
    [path stroke];

1 个答案:

答案 0 :(得分:1)

  

我解决了以下问题

// Main Path
UIBezierPath *path = [UIBezierPath bezierPath];

// Line Path
UIBezierPath *linePath = [UIBezierPath bezierPath];

// Rect path
UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:rect];
[path appendPath:rectPath];

// 9 circle path
[self.subviews enumerateObjectsUsingBlock:^(PPSingleCircle * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:obj.frame];
    [path appendPath:circlePath];
}];

// clip the path
path.usesEvenOddFillRule = YES;
[path addClip];
// stroke the "Main path" first
[path stroke];

// add line to each select Views
for (int i = 0; i < [self.selectViews count]; i++) {

    PPSingleCircle *singleView = self.selectViews[i];
    if (i == 0) {
        [linePath moveToPoint:singleView.center];
    }else {
        [linePath addLineToPoint:singleView.center];
    }
}

// add line to current point
PPSingleCircle *view = self.selectViews[0];
if (view.status != CircleStatusError) {
    [linePath addLineToPoint:self.currentPoint];
}

// set display style
[kLineColor setStroke];
linePath.lineWidth = kLineWidth;
linePath.lineJoinStyle = kCGLineJoinRound;
linePath.lineCapStyle  = kCGLineCapRound;

// add line path and draw
[linePath stroke];