使UIView的特定角落成圆形

使UIView的特定角落成圆形

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

问题描述

我做了一些研究,我发现,没有简单的方法可以让
仅在UIView回合的右上角和左上角,对吗?



ps。这段代码使得所有4个角都是圆形的。我不想要。

  #import< QuartzCore / QuartzCore.h> 

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


解决方案

您可以使用此代码段执行此操作:

  //设置右上角&左角
[self setMaskTo:yourView byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight];

编辑



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

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


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

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

EDIT

Sorry, I forgot this

- (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: 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的特定角落成圆形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 20:14