[转]ios之如何让图片显示成圆形的样式/设置控件边框大小以及颜色

比如说QQ登陆头像显示出来的就是圆形的,但实际上它的图片并非就是圆形,而是通过对layer层进行绘制而成的。说到layer每个控件都会有layer层属性所以可以把任意的控件都可以设置成圆形,或是椭圆型看项目需要而定

 

1 UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"头像.png"]];  
2    imageView.frame = CGRectMake(100, 100, 100, 100);  
3    // 通过layer层设置控件的边角半径(每个控件都会有一个layer层所有其他控件也都能实现圆形效果)  
4    imageView.layer.cornerRadius = 50;  
5    // 同时也要设置超出部分不让显示 这样就可以达到一个圆形的效果图片-  
6    imageView.clipsToBounds = YES;  
7      
8    [mainView.view addSubview:imageView]; 

 


再来看看layer层还带有什么属性,可以设置什么效果呢,一个是可以设置控件的边框大小以及颜色,有时候可以方便我们对项目控件的测试说白了也就是更加清晰显示出控件的位置把比如说tableView 的cell 设置后可以很清楚看到每个cell的大小范围,其次还可以实现点击让控件有选中的效果,更加高级点就是通过layer层对控件进行动画的绘制

 

下面是苹果API

 

 1 @property CGFloat cornerRadius; //设置边角半径  
 2   
 3 /* The width of the layer's border, inset from the layer bounds. The 
 4  * border is composited above the layer's content and sublayers and 
 5  * includes the effects of the `cornerRadius' property. Defaults to 
 6  * zero. Animatable. */  
 7   
 8 @property CGFloat borderWidth; // 设置控件的边框宽度  
 9   
10 /* The color of the layer's border. Defaults to opaque black. Colors 
11  * created from tiled patterns are supported. Animatable. */  
12   
13 @property CGColorRef borderColor;  // 设置控件的边框颜色  
14   
15 /* The opacity of the layer, as a value between zero and one. Defaults 
16  * to one. Specifying a value outside the [0,1] range will give undefined 
17  * results. Animatable. */  
18   
19 @property float opacity; // 设置控件的透明度有部分动画会用到  
20   
21 /* When true, and the layer's opacity property is less than one, the 
22  * layer is allowed to composite itself as a group separate from its 
23  * parent. This gives the correct results when the layer contains 
24  * multiple opaque components, but may reduce performance. 
25  * 
26  * The default value of the property is read from the boolean 
27  * UIViewGroupOpacity property in the main bundle's Info.plist. If no 
28  * value is found in the Info.plist the default value is YES for 
29  * applications linked against the iOS 7 SDK or later and NO for 
30  * applications linked against an earlier SDK. */  
1 // 只需要这两句话就可以设置完成  
2     imageView.layer.borderWidth = 2; // float类型数据  
3     imageView.layer.borderColor = [UIColor redColor].CGColor;  // 颜色需要是CGColor 

 

posted @ 2016-02-04 15:44  CUG  阅读(506)  评论(0编辑  收藏  举报