【问题标题】:External Fill Color in iOSiOS中的外部填充颜色
【发布时间】:2013-05-15 18:22:15
【问题描述】:

我有两个矩形(两个封闭子cgpaths)。 矩形 B 很小,并且存在于矩形 A 内。其中的所有边。 是否有直接的方法来填充矩形 B 外部的颜色区域。

CAShapeLayer fillExternalColor 类似的东西?如果不是直接方式,如何以编程方式执行此操作?

A - 紫色 B - 黄色

所以我尝试画 A,然后画 B。我希望 B 是清晰的颜色(现在我设置为黄色)但后来我看到 A 的紫色..

我找到了给出 AnB 的 CGRectIntersection 方法和给出 AuB 的 CGRectUnion 方法。 有没有一种方法可以给出 AuB - AnB 的其余区域?

【问题讨论】:

  • 你说的是在矩形外部绘制,对吧?
  • 你只是想填充矩形A?您需要以某种方式知道外部界限...
  • @Undo yes... 在 B 之外但在 A 内绘制颜色
  • 你为什么不填充A,然后在上面画B?
  • @thelaws ...请看截图..

标签: iphone ios cgpath cashapelayer


【解决方案1】:

您使用的是CAShapeLayer。如果您的fillColor 和您的strokeColor 都是不透明的(alpha 1.0),您可以简单地设置图层的backgroundColor 来填充那些在图层边界内但在其描边和填充路径之外的像素。

我的测试代码:

@implementation ViewController {
    CAShapeLayer *layer;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    layer = [CAShapeLayer layer];
    layer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 250, 250)].CGPath;
    layer.fillColor = [UIColor yellowColor].CGColor;
    layer.strokeColor = [UIColor whiteColor].CGColor;
    layer.lineWidth = 4;
    layer.backgroundColor = [UIColor purpleColor].CGColor;
    [self.view.layer addSublayer:layer];
}

- (void)viewDidLayoutSubviews {
    layer.frame = self.view.bounds;
}

@end

结果:

【讨论】:

    【解决方案2】:

    我想在 UIImage 上添加红色边框内部矩形,外部蒙面。

    我是通过这个页面来的。以下代码使用 CGContextEOFillPath 可以对像我这样的其他人有所帮助。 (部分代码是从其他页面收集的。)

        -(UIImage ) imageByDrawingBorderRectOnImage:(UIImage )image theRect:(CGRect)theRect
        {
            // begin a graphics context of sufficient size
            UIGraphicsBeginImageContext(image.size);
    
            // draw original image into the context
            [image drawAtPoint:CGPointZero];
    
            // get the context for CoreGraphics
            CGContextRef ctx = UIGraphicsGetCurrentContext();
    
            // set stroking color and to draw rect
            [[UIColor redColor] setStroke];
    
            // drawing with a red stroke color
            CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    
            // the line width to 3
            CGContextSetLineWidth(ctx, 3.0);
    
            // Add Stroke Rectangle,
            CGContextStrokeRect(ctx, theRect);
    
            // Now draw fill outside part with partial alpha gray color
            // drawing with a gray stroke color
            CGMutablePathRef aPath = CGPathCreateMutable();
            // outer rectangle
            CGRect rectangle = CGRectMake( 0, 0, image.size.width, image.size.height);
            CGPathAddRect(aPath, nil, rectangle);
            // innter rectangle
            CGPathAddRect(aPath, nil, theRect);
            // set gray transparent color
            CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:0.75 green:0.75 blue:0.75 alpha:0.5].CGColor);
            // add the path to Context
            CGContextAddPath(ctx, aPath);
            // This method uses Even-Odd Method to draw in outer rectangle
            CGContextEOFillPath(ctx);
    
            // make image out of bitmap context
            UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();
    
            // free the context
            UIGraphicsEndImageContext();
    
            return retImage;
        }
    

    问候。

    【讨论】:

      【解决方案3】:

      假设 A 是较大的形状,B 是较小的内部形状,并且 bounds 是指 A 的包含矩形(可能是视图的边界)

      1. 将形状 B 剪辑到上下文中(-addClip 应该这样做)
      2. CGContextClearRect界限
      3. 填充形状 A

      【讨论】:

      • 您说 addClip 时能详细说明一下吗?
      • 很抱歉,我无法遵循您的建议。我有一个 CGRect innerRect 和 CGRect outerRect。我正在一个单独的方法中绘制东西,该方法从主线程 dispatch_sync(dispatch_get_main_queue(), ^{ [self displayRectangles:rectangles withLineColor:lineColor]; });稍后我创建了一个 CAShapeLayer 并将该层作为子层添加到视图中。
      【解决方案4】:

      编辑,找到source of this code

      给定HoleView : UIView,您必须设置:

      self.opaque = NO;
      self.backgroudColor = [UIColor clearColor];
      

      在drawRect:

      [[UIColor purpleColor] setFill];
      UIRectFill(A);
      
      CGRect gapRectIntersection = CGRectIntersection(B, A);    
      [[UIColor clearColor] setFill];
      UIRectFill(gapRectIntersection);
      

      【讨论】:

        【解决方案5】:

        kCAFillRuleEvenOdd

        指定奇偶缠绕规则。计算路径交叉的总数。如果交叉的数量是偶数,则该点在路径之外。如果交叉的数量是奇数,则该点在路径内,包含它的区域应该被填充。

        在 OS X v10.6 及更高版本中可用。

        在 CAShapeLayer.h 中声明。

        参考:CAShapeLayer Class reference

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-10-27
          • 2012-04-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-10
          • 1970-01-01
          相关资源
          最近更新 更多