我试图使用" UIBezierPath"来绕过我的视角。我只需要围绕topRight和左上角。
我使用了以下代码
-(void)setMaskOnView:(UIView *)givenView
{
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:givenView.bounds byRoundingCorners: (UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = givenView.bounds;
maskLayer.path = maskPath.CGPath;
givenView.layer.mask = maskLayer;
}
但是我的TopRight Corner不会圆。
我已经
了UIRectCornerAllCorners
但它没有围绕我的右角
我错过了什么?
答案 0 :(得分:1)
我建议采用不同的方法。加载带有圆角顶角和图像的图像为CALayer
的内容。将此图层设置为视图图层的蒙版。更新给定视图的layoutSubivews
或给定视图控制器的viewDidLayoutSubviews
中的遮罩层大小。
将图片加载为图层有用
CALayer *maskLayer = [[CALayer alloc] init];
UIImage *maskImage = [UIImage imageNamed:@"mask_image.png" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
maskLayer.contents = (__bridge id _Nullable)(maskImage.CGImage);
mainLayer.mask = maskLayer
[编辑]在评论中回答您的问题
使用CAShapeLayer或图像作为遮罩,您必须调整遮罩层的大小,使其与遮罩层的大小相同。如果我们正在谈论UITableViewCell
创建您自己的派生单元格并更新layoutSubviews
中的蒙版形状。下面是示例代码(从故事板加载MyTableCell):
@interface MyTableCell ()
@property (nonatomic, strong) CAShapeLayer *maskLayer;
@end
@implementation MyTableCell
- (void)awakeFromNib
{
self.maskLayer = [[CAShapeLayer alloc] init];
self.layer.mask = self.maskLayer;
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.maskLayer.path = [self maskPath].CGPath;
}
- (UIBezierPath *)maskPath
{
return [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners: (UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(10.0, 10.0)];
}
@end
答案 1 :(得分:0)
我正在使用这个子类
·H
@interface UIView (custom)
- (UIView *)setRoundedCorners:(UIRectCorner)corners withRadius:(CGFloat)radius;
@end
的.m
@implementation UIView (custom)
- (UIView *)setRoundedCorners:(UIRectCorner)corners withRadius:(CGFloat)radius
{
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
return self;
}
@end
使用它像:
[YOURVIEW setRoundedCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerTopLeft | UIRectCornerTopRight withRadius:15];