使 UIView 的特定角变圆 [英] Make specific corners of UIView round

查看:20
本文介绍了使 UIView 的特定角变圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些研究,我明白了,没有简单的方法可以使 UIView 的右上角和左上角变成圆形,对吗?

I did a bit of research, and I see, there is no easy way to make only top right and top left corners of the UIView round, right?

ps.此代码使所有 4 个角都变圆,这很简单.我不要.

ps. This code makes all 4 corners round which is smth. I don't want.

  #import <QuartzCore/QuartzCore.h>

 self.layer.cornerRadius = 5;
 self.layer.masksToBounds = YES;

推荐答案

你可以用这个片段来做到:

You can do it with this snippet:

// Set top right & left corners
[self setMaskTo:yourView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];

编辑

对不起,我忘记了

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

PS:正如我在评论中所说,如果你在显示 UITableViewCell 单元格时使用这样的东西,我发现使用背景图像总是更好的选择,因为这种绘图的东西总是会影响性能.

PS: as I said in the comment, if you use things like this when displaying UITableViewCell cells, I've found that it's always a better choice to use background images because this kind of drawing stuff always affects performance.

这篇关于使 UIView 的特定角变圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆