1

I want to change the color of the intersection of the two circles.

I now use this method:

- (void)drawRect:(CGRect)rect{
    // draw circle 1
    CGRect circle1=CGRectMake(100, 200, 80, 80);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddArc(ctx, CGRectGetMinX(circle1), CGRectGetMinY(circle1), CGRectGetWidth(circle1)/2, 0, 2*M_PI, 1);
    CGContextSetFillColorWithColor(ctx, [UIColor blackColor].CGColor);
    CGContextDrawPath(ctx, kCGPathFill);

    // draw circle 2
    CGRect circle2=CGRectMake(170, 200, 80, 80);
    CGContextAddArc(ctx, CGRectGetMinX(circle2), CGRectGetMinY(circle2), CGRectGetWidth(circle2)/2, 0, 2*M_PI, 1);
    CGContextSetFillColorWithColor(ctx, [UIColor blackColor].CGColor);
    CGContextDrawPath(ctx, kCGPathFill);

    // intersection Rect
    CGRect intersectionRect=CGRectIntersection(circle1, circle2);
}

to get the intersection location, but what to do next?

As shown, I want to change the intersection of the two black circles to white.

intersection image

rmaddy
  • 314,917
  • 42
  • 532
  • 579
ch.chay
  • 21
  • 2

1 Answers1

0

If they are circles.

If they are recatangles:

    let v1 = UIView()
    v1.frame = CGRect(x: 50, y: 50, width: 100, height: 100)
    v1.backgroundColor = UIColor.blue
    self.view.addSubview(v1)

    let v2 = UIView()
    v2.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
    v2.backgroundColor = UIColor.yellow
    self.view.addSubview(v2)

    let v3 = UIView()
    v3.frame = v1.frame.intersection(v2.frame)
    v3.backgroundColor = UIColor.green
    self.view.addSubview(v3)
Anters Bear
  • 1,816
  • 1
  • 15
  • 41
  • This will not work. This will change the color outside the circle, – ch.chay May 19 '18 at 04:08
  • Sry question was edited before you didn't mention circles you just said views. It will be much more complicated to do with circles / shapes... – Anters Bear May 19 '18 at 04:14