drawing with clear color on UIView (cutting a hole) in static method

▼魔方 西西 提交于 2020-01-12 23:12:09

问题


I have an iPhone application and I need to implement the following method:

+(UITextView *)textView:(UITextView *) withCuttedRect:(CGRect)r

This method must cut (fill with [UIColor clearColor]) the rect r from UITextView and return UITextView object.

The user will see the view behind UITextView from the cutted holes.

How can it be done?


回答1:


When you would have something like:

 +(UITextView *)textView:(UITextView *)textView withCuttedRect:(CGRect)r {
}

you actually can simply access the textview's layer from core animation with

 textView.layer

What you then can to is set a mask for clipping. These masks work the following way: you generally draw a black shape, and that stays the same, the rest will be clipped (ok, you actually can also do some things on the alpha channel but roughly that is it).

So you need a black rectangle as a mask, with a rectangle within the rectangle which is free. for that you can approximately do

 CAShapeLayer *mask = [[CAShapeLayer alloc] init];
 mask.frame = self.textView.layer.bounds;
 CGRect biggerRect = CGRectMake(mask.frame.origin.x, mask.frame.origin.y, mask.frame.size.width, mask.frame.size.height);
 CGRect smallerRect = CGRectMake(50.0f, 50.0f, 10.0f, 10.0f);

 UIBezierPath *maskPath = [UIBezierPath bezierPath];
[maskPath moveToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMinY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMaxY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(biggerRect), CGRectGetMaxY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(biggerRect), CGRectGetMinY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMinY(biggerRect))];

[maskPath moveToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMinY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMaxY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(smallerRect), CGRectGetMaxY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(smallerRect), CGRectGetMinY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMinY(smallerRect))];

 mask.path = maskPath.CGPath;
[mask setFillRule:kCAFillRuleEvenOdd];
 mask.fillColor = [[UIColor blackColor] CGColor];
 self.textView.layer.mask = mask;

The code above is also disused by Crop a CAShapeLayer retrieving the external path

The idea why that filling works that way is nicely explained in Quartz 2D Programming Guide in the section "Filling a Path"



来源:https://stackoverflow.com/questions/11013600/drawing-with-clear-color-on-uiview-cutting-a-hole-in-static-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!