圆顶右上角和右下角

时间:2017-09-20 10:08:25

标签: ios objective-c iphone uiview

我需要围绕我的图片视图和uiview的特定角落 Imgview左上角和右上角 查看左下方和右下方。

我探索并找到了这个方法

[self setMaskTo:_viewBookNow byRoundingCorners:UIRectCornerBottomLeft];
[self setMaskTo:_viewBookNow byRoundingCorners:UIRectCornerBottomRight|UIRectCornerBottomRight];

[self setMaskTo:_imgVAnimal byRoundingCorners:UIRectCornerTopRight];
[self setMaskTo:_imgVAnimal byRoundingCorners:UIRectCornerTopLeft];

方法定义是这个

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                              byRoundingCorners:corners
                                                    cornerRadii:CGSizeMake(8.0, 8.0)];
CAShapeLayer *shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
view.layer.mask = shape;
}

但它只围绕视图的左角和图像不适用于右角。

请建议

谢谢

1 个答案:

答案 0 :(得分:0)

  

目标C解决方案 -

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
    UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                                  byRoundingCorners:corners
                                                        cornerRadii:CGSizeMake(10.0, 10.0)];
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    view.layer.mask = shape;
}

并像这样使用它 -

[self setMaskTo:viewToRound byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
  

Swift Solution -

请先在您的文件中创建此扩展程序

extension UIView {
    func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}

然后创建UIView的自定义子类并在该类中写入这些行

override func layoutSubviews() {
    super.layoutSubviews()
    self.roundCorners([.topLeft, .bottomRight], radius: 50)
}

根据需要在数组中设置这些特定角点。

还在故事板中或以编程方式设置clipToBounds = true。

相关问题