ios设置圆角在覆盖drawRect后不起作用,但普通UIView正常
我写了一个关于Wave动画的例子。动画还可以,但我不明白为什么自定义UIView需要添加“self.layer.masksToBounds=YES”以获得圆角ios设置圆角在覆盖drawRect后不起作用,但普通UIView正常,ios,objective-c,uiview,calayer,drawrect,Ios,Objective C,Uiview,Calayer,Drawrect,我写了一个关于Wave动画的例子。动画还可以,但我不明白为什么自定义UIView需要添加“self.layer.masksToBounds=YES”以获得圆角 这是一个自定义UIView。我重写了它的草稿。如果我不将“masksToBounds”设置为“是”,则圆角将消失 - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { sel
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.frame = CGRectMake(10, 40, 300, 300);
self.layer.cornerRadius = 150;
self.backgroundColor = [UIColor greenColor];
self.layer.borderColor = [UIColor blueColor].CGColor;
self.layer.borderWidth = 2;
// self.layer.masksToBounds = YES; //if write this line,the round corner appear
self.x_move = 0;
self.y_move = 300;
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
CGContextSetLineWidth(context, 1);
CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
CGPathMoveToPoint(path, NULL, 0, 0);
for(float i=0; i<=300; i++){
float x=i;
float y = 5 * sin( 0.05 * x+ self.x_move) + self.y_move;
CGPathAddLineToPoint(path, nil, x, y);
}
CGPathAddLineToPoint(path, nil, 300, 0);
CGPathAddLineToPoint(path, nil, 0, 0);
CGContextAddPath(context, path);
CGContextFillPath(context);
CGContextDrawPath(context, kCGPathStroke);
CGPathRelease(path);
}
- (void)startAnimation {
if (self.waveTimer == nil) {
self.waveTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(refresh) userInfo:nil repeats:YES];
}
}
- (void)refresh {
self.x_move += 0.3;
self.y_move -= 0.2;
if(self.y_move - 100 < 0.00001){
[self.waveTimer invalidate];
}else{
[self setNeedsDisplay];
}
}
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 60, 100, 100)];
view.backgroundColor = [UIColor greenColor];
view.layer.cornerRadius = 50;
view.layer.borderWidth = 2;
view.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview:view];
这是因为您重写了
drawRect:(CGRect)frame
您正在尝试设置图层的角半径,该图层不是指定填充颜色的元素。之所以设置masksToBounds
可以获得所需的圆角,是因为在这里您告诉视图将其所有层屏蔽到帧的边界
在第二个使用UIView
的示例中,角半径显示为预期值,因为您没有调整其图层
我建议您采用设置masksToBounds
,或者如果您需要masksToBounds=NO
,则以不同的方式重新绘制视图的边界。因为你有“蠕动”正弦曲线,你想要圆角,也许只需要创建一个通用的圆角矩形(一个具有所需的角半径),然后将其与你已经用正弦波创建的路径合并
该链接可以帮助您将圆角视图与自定义视图合并,以获得所需的最终结果
否则。。可能最好是将其设置为“是”。谢谢您的回答!关键信息是我自己在drawRect中绘制的层和self.layer是不同的层。我认为在覆盖“drawRect”时创建一个新层,因此设置“self.layer”并不等于设置我自己绘制的层。这是对的吗?大体上我相信是的,你说的是对的。在你的问题上真的没什么坏处,这只是一个“幕后”的技术问题。尝试使用所需路径编辑图层本身(self.layer),或使用
masksToBounds
。希望我的回答能帮助你找到解决方案!如果你觉得它确实存在,请接受apple document--cornerRadius“它只适用于图层的背景色和边框。但是,将masksToBounds属性设置为YES会导致内容被剪裁到圆角”,我以前不理解,我认为覆盖“drawRect”这意味着我正在调整图层的内容。因此,cornerRadius不起作用,因为它仅适用于图层的背景色和边框。正确。就像我说的,使用mask属性没有什么害处,只是有助于您理解为什么。像这样的事情在解决之前总是令人讨厌的
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 60, 100, 100)];
view.backgroundColor = [UIColor greenColor];
view.layer.cornerRadius = 50;
view.layer.borderWidth = 2;
view.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview:view];