如何制作任何视图的圆形

我只是想改变广场到圆形视图的形状。 当我尝试与angular落,但只是在angular落里。 因为我想把整个视图看成圆形。

UIView总是矩形的。 但是,你可以用面具使它看上去很圆(或者任何形状)。 为此,请创build一个黑色圆圈的UIImage(在透明背景中)。 现在把这个UIImage的CGImage作为一个CALayer的内容。 最后,将CALayer设置为视图图层的mask

让我们假设你的观点是100×100然后(没有testing,但应该是非常正确的):

 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; 

你可以通过这种方式使任何控件的边框宽度为圆形:

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

只要用你的控件replaceviewPopup

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

试试这个代码:

 [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是你的观点,你想要四舍五入。

我希望它可以帮助你。 谢谢