我正在尝试覆盖drawRect:
我的自定义视图中的 UIView 方法。但是,我的视图的边界半径定义为:
sub = [[[NSBundle mainBundle] loadNibNamed:@"ProfileView" owner:self options:nil] objectAtIndex:0];
[self addSubview:sub];
[sub setUserInteractionEnabled:YES];
[self setUserInteractionEnabled:YES];
CALayer *layer = sub.layer;
layer.masksToBounds = YES;
layer.borderWidth = 5.0;
layer.borderColor = [UIColor whiteColor].CGColor;
layer.cornerRadius = 30.0;
这完美地工作并在我的视图周围放置了一个带有边框半径的漂亮边框(不要介意后面的对角线/直白线,它们与此视图无关):
但是,当我尝试覆盖drawRect:
在我看来,使用这种方法,我可以看到黑色背景没有遮盖边界。我不做anything(目前),这是我的代码:
-(void)drawRect:(CGRect)rect{
[super drawRect:rect];
}
这是结果:
除了绘制方法之外,我什么也没改变。如何覆盖绘制方法,同时保持我的视图遵循角半径蒙版?这是 iOS 中的错误还是我遗漏了什么?
我不知道完整的答案,但我确实知道 UIView 的实现drawLayer:inContext:
工作方式有所不同,具体取决于您是否已实施drawRect:
或不。也许屏蔽/剪切边界是它所做的不同的事情之一。
您可以尝试通过多种方式解决您的问题:
-
让你的背景透明:
layer.backgroundColor = [UIColor clearColor].CGColor;
-
把自己夹在你的自定义里面drawRect:
:
- (void)drawRect:(CGRect)rect {
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:30.0] addClip];
[image drawInRect:rect]; // or whatever
}
-
明确地切出角落:
CGContextBeginPath(c);
CGContextAddArc(c, r, r, r, M_PI, 3*M_PI_2, 0);
CGContextAddLineToPoint(c, 0, 0);
CGContextClosePath(c);
CGContextClip(c);
[[UIColor grayColor] setFill];
UIRectFill(rect);
我从 WWDC 2010 的精彩演讲中借鉴了最后两条建议:iPhone 操作系统上的高级性能优化 http://adcdownload.apple.com//wwdc_2010/wwdc_2010_video_assets__pdfs/135__advanced_performance_optimization_on_iphone_os_part_1.pdf(视频列出于这个索引页 https://developer.apple.com/videos/wwdc/2010/——令人烦恼的是,没有直接链接)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)