问题描述
我只想将视图的形状从Square更改为Round。当我尝试使用cornerRadious时,它只是在拐角处。因为我想将整个视图视为圆形。
I just want to change the shape of view from Square to Round. As i try with cornerRadious, But it just round the corner only. As I want to make whole view as a round shape.
推荐答案
UIView始终是矩形的。但是,您可以使用蒙版使其看起来圆形(或任何形状)。为此,请创建一个黑色圆圈的UIImage(在透明背景中)。现在获取该UIImage的CGImage并将其作为CALayer的内容。最后,将CALayer设置为视图图层的掩码
。
A UIView is always rectangular. However, you can make it look round (or any shape at all, actually) with a mask. To do so, make a UIImage which is a black circle (in a transparent background). Now take the CGImage of that UIImage and make it the contents of a CALayer. Finally, set that CALayer as the mask
of the layer of your view.
让我们假设您的视图是100 x 100.然后(未测试,但应该非常正确):
Let us suppose that your view is 100 x 100. Then (not tested, but should be pretty much right):
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;
这篇关于如何制作任何视图的圆形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!