나는 석영 및 클리핑 영역에 대해 질문이 있습니다 :석영 및 클리핑 영역
나는 사각형 나는 B의 dveve의 충전도 잘라 사각형 B 을하고 싶은이 사각형 안에 을하고 싶습니다 A : B가 A를 뚫고 싶습니다. 석영에서 가장 좋은 방법은 무엇입니까? 나는 정말로 어떻게 클리핑을 이해하지 못했습니다
나는 석영 및 클리핑 영역에 대해 질문이 있습니다 :석영 및 클리핑 영역
나는 사각형 나는 B의 dveve의 충전도 잘라 사각형 B 을하고 싶은이 사각형 안에 을하고 싶습니다 A : B가 A를 뚫고 싶습니다. 석영에서 가장 좋은 방법은 무엇입니까? 나는 정말로 어떻게 클리핑을 이해하지 못했습니다
내가이 간단한 방법으로 해결 :
- (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);
}
정확하게 이해한다면, 더 큰 직사각형 안에 더 작은 직사각형을 그려 내부 직사각형이 투명 해 지도록 할 수 있습니다. 두 구형을 하위 경로로 포함하는 경로로 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];