为了账号安全,请及时绑定邮箱和手机立即绑定

如何为UIView的左上角和右上角设置角半径?

如何为UIView的左上角和右上角设置角半径?

狐的传说 2019-06-29 15:03:55
如何为UIView的左上角和右上角设置角半径?有没有办法cornerRadius的左上角和右上角UIView?我试过以下几种方法,但最后都看不到风景了。UIView *view = [[UIView alloc] initWithFrame:frame];CALayer *layer = [CALayer layer];UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRoundedRect:frame byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(3.0, 3.0)];layer.shadowPath = shadowPath.CGPath;view.layer.mask = layer;
查看完整描述

3 回答

?
森林海

TA贡献2011条经验 获得超2个赞

SWIFT 4

请注意这样一个事实,即如果您的UIView子类中附加了布局约束,则必须按如下方式刷新它:

override func layoutSubviews() {
    super.layoutSubviews()
    roundCorners(corners: [.topLeft, .topRight], radius: 3.0)}

如果你不这么做,它就不会出现。


在圆角处,使用分机:

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


查看完整回答
反对 回复 2019-06-29
?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

我不知道为什么您的解决方案不起作用,但下面的代码对我有用。创建一个Bezier掩码并将其应用到您的视图中。在下面的代码中,我正在舍入_backgroundView半径为3像素。self是一种习俗UITableViewCell:

UIBezierPath *maskPath = [UIBezierPath
    bezierPathWithRoundedRect:self.backgroundImageView.bounds
    byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
    cornerRadii:CGSizeMake(20, 20)];CAShapeLayer *maskLayer = [CAShapeLayer layer];maskLayer.frame = self.bounds;maskLayer.path = maskPath.CGPath;self.backgroundImageView.layer.mask = maskLayer;

有一些改进的SWIFT版本:

let path = UIBezierPath(roundedRect:viewToRound.bounds, byRoundingCorners:[.TopRight, .BottomLeft], cornerRadii: CGSizeMake(20, 20))let maskLayer = CAShapeLayer()maskLayer.path = path.CGPathviewToRound.layer.mask = maskLayer

SWIFT 3.0版本:

let path = UIBezierPath(roundedRect:viewToRound.bounds,
                        byRoundingCorners:[.topRight, .bottomLeft],
                        cornerRadii: CGSize(width: 20, height:  20))let maskLayer = CAShapeLayer()maskLayer.path = path.cgPath
viewToRound.layer.mask = maskLayer


查看完整回答
反对 回复 2019-06-29
?
四季花海

TA贡献1811条经验 获得超5个赞

这里有一个斯威夫特@JohnnyRockex答案的版本

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    }}

view.roundCorners([.topLeft, .bottomRight], radius: 10)

如果你用自动布局,您需要将您的UIView打电话roundCorners在视野中layoutSubviews为了达到最佳效果。

class View: UIView {
    override func layoutSubviews() {
        super.layoutSubviews()

        self.roundCorners([.topLeft, .bottomLeft], radius: 10)
    }}


查看完整回答
反对 回复 2019-06-29
  • 3 回答
  • 0 关注
  • 849 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信