如何制作任何视图的圆形

时间:2013-05-10 05:25:17

标签: iphone ios user-interface uiview

我只想将视图的形状从Square更改为Round。当我尝试使用cornerRadious时,它只是在拐角处。因为我想把整个视图看成圆形。

3 个答案:

答案 0 :(得分:6)

UIView总是长方形的。但是,您可以使用蒙版使其看起来圆形(或任何形状)。为此,请创建一个黑色圆圈的UIImage(在透明背景中)。现在获取该UIImage的CGImage并将其作为CALayer的内容。最后,将CALayer设置为视图图层的mask

让我们假设您的视图是100 x 100.然后(未经测试,但应该非常正确):

UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,100), NO, 0);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(c, [UIColor blackColor].CGColor);
CGContextFillEllipseInRect(c, CGRectMake(0,0,100,100));
UIImage* maskim = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CALayer* mask = [CALayer new];
mask.frame = CGRectMake(0,0,100,100);
mask.contents = (id)maskim.CGImage;

view.layer.mask = mask;

答案 1 :(得分:2)

您可以这样使用任何控件的边框宽度制作圆角边框: -

CALayer * l1 = [viewPopup layer];
[l1 setMasksToBounds:YES];
[l1 setCornerRadius:5.0];

// You can even add a border
[l1 setBorderWidth:5.0];
[l1 setBorderColor:[[UIColor darkGrayColor] CGColor]];


只需用您的控件替换viewPopup即可。

注意: - 不要忘记导入<QuartzCore/QuartzCore.h>

答案 2 :(得分:1)

试试这段代码: -

[roundView.layer setCornerRadius:50.0f];
[roundView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[roundView.layer setBorderWidth:1.5f];
[roundView.layer setShadowColor:[UIColor blackColor].CGColor];
[roundView.layer setShadowOpacity:0.8];
[roundView.layer setShadowRadius:3.0];
[roundView.layer setShadowOffset:CGSizeMake(2.0, 2.0)];

注意: - roundView是您想要舍入的视图。

我希望它可以帮到你。感谢