【发布时间】:2015-02-11 15:30:55
【问题描述】:
我正在使用此代码使我的顶角变圆。
- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
view.layer.mask = shape;
}
然后在 superview 方法的 initWithFrame 中我做了这个:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
[self setMaskTo:imageView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
但结果是我看不到图像。我正在使用正确的自动布局约束,如果我不使用上面的圆角方法,我会看到图像。但是如果我使用它,我看不到图像。
似乎如果使用放置图像的视图的绘制矩形回调,我可以设置圆角。但是在这种情况下,如果视图包含其他视图,它将为所有子视图添加舍入,如果我将循环它:
当然,它适用于所有子视图:
- (void)drawRect:(CGRect)rect {
// Drawing code
for (UIView *view in self.subviews)
{
[self setMaskTo:view byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
}
}
- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
view.layer.mask = shape;
}
那么如何检测图像视图何时有框架,我认为框架在开始时为 nil CALayer 不了解如何与视图交互。
【问题讨论】:
标签: ios ios7 ios8 calayer cashapelayer