【问题标题】:Round top right and bottom right corners of view圆形视图的右上角和右下角
【发布时间】:2018-03-01 08:18:17
【问题描述】:

我需要围绕我的图像视图和 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;
}

但它仅适用于视图的左角,而图像不适用于右角。

请推荐

谢谢

【问题讨论】:

  • 使用clipsToBounds = true & maskstobounds = true 试试
  • 属性掩码边界不是为了查看。显示此错误。
  • 不工作.....
  • 使用view.layer.masktobounds = true它是一个图层属性。

标签: ios objective-c iphone uiview


【解决方案1】:

Objective 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];

快速解决方案 -

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

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。

【讨论】:

  • 感谢 Prateek 我需要客观的 C 代码,我已经尝试过我在我的问题中发布过的相同的代码,您可以建议任何其他解决方案或永久有效的解决方案
  • 哦。一定让我看到并在一段时间内回复你
  • 我试过你的代码,经过一点修改它就可以工作了。您在方法中经过了相同的角落,这就是它不起作用的原因。我已经更新了我的答案,它包含了 Objective C 和 Swift 代码。如有任何帮助,请告诉我。
猜你喜欢
  • 1970-01-01
  • 2021-08-21
  • 2013-12-22
  • 2013-05-10
  • 2021-05-04
  • 2021-01-13
  • 2017-09-13
  • 2012-02-17
  • 1970-01-01
相关资源
最近更新 更多