【问题标题】:Quartz and clipping area石英和剪裁区域
【发布时间】:2013-08-11 18:53:12
【问题描述】:

我有一个关于 Quartz 和裁剪区域的问题:

我想要一个矩形 A 在这个矩形内我想要一个矩形 B B dveve 的填充物也切入 A:我希望 A 被 B 刺穿。在石英中做到这一点的最佳方法是什么?我真的不明白剪辑是怎么回事

【问题讨论】:

    标签: ios drawrect quartz-2d clipping


    【解决方案1】:

    如果我理解正确,您想在一个较大的矩形内绘制一个较小的矩形,以便内部矩形是透明的。您可以通过绘制一个包含两个矩形作为子路径的路径的 CAShapeLayer 来实现这一点。不要忘记将图层的填充规则设置为kCAFillRuleEvenOdd

    试试这样的:

    CGRect rectA = CGRectMake(100, 100, 200, 200);
    CGRect rectB = CGRectMake(150, 150, 100, 100);
    
    UIBezierPath *path=[[UIBezierPath alloc] init];
    
    // Add sub-path for rectA
    [path moveToPoint:CGPointMake(rectA.origin.x, rectA.origin.y)];
    [path addLineToPoint:CGPointMake(rectA.origin.x+rectA.size.width, rectA.origin.y)];
    [path addLineToPoint:CGPointMake(rectA.origin.x+rectA.size.width, rectA.origin.y+rectA.size.height)];
    [path addLineToPoint:CGPointMake(rectA.origin.x, rectA.origin.y+rectA.size.height)];
    [path closePath];
    
    // Add sub-path for rectB
    [path moveToPoint:CGPointMake(rectB.origin.x, rectB.origin.y)];
    [path addLineToPoint:CGPointMake(rectB.origin.x+rectB.size.width, rectB.origin.y)];
    [path addLineToPoint:CGPointMake(rectB.origin.x+rectB.size.width, rectB.origin.y+rectB.size.height)];
    [path addLineToPoint:CGPointMake(rectB.origin.x, rectB.origin.y+rectB.size.height)];
    [path closePath];
    
    // Create CAShapeLayer with this path
    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    [pathLayer setFillRule:kCAFillRuleEvenOdd]; /* <- IMPORTANT! */
    [pathLayer setPath:path.CGPath];
    [pathLayer setFillColor:[UIColor blackColor].CGColor];
    
    // Add the CAShapeLayer to a view
    [someView.layer addSublayer:pathLayer];
    

    【讨论】:

      【解决方案2】:

      我用这个简单的方法解决了:

      - (void)drawRect:(CGRect)rect
      {
          CGContextRef context = UIGraphicsGetCurrentContext();
          CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0);
          CGContextFillRect(context,self.bounds);
          CGContextAddRect(context, self.bounds);
          //Add cropped rectangle:
          CGContextAddRect(context, _croppedRegion);
          //Clip:
          CGContextEOClip(context);
          CGContextSetRGBFillColor(context, 255.0, 255.0, 255.0, 0.5);
          CGContextFillRect(context, self.bounds);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-01
        相关资源
        最近更新 更多