標籤:ios之如何讓圖片顯示成圓形的樣式設定控
比如說QQ登陸頭像顯示出來的就是圓形的,但實際上它的圖片並非就是圓形,而是通過對layer層進行繪製而成的。說到layer每個控制項都會有layer層屬性所以可以把任意的控制項都可以設定成圓形,或是橢圓型看項目需要而定
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"頭像.png"]]; imageView.frame = CGRectMake(100, 100, 100, 100); // 通過layer層設定控制項的邊角半徑(每個控制項都會有一個layer層所有其他控制項也都能實現圓形效果) imageView.layer.cornerRadius = 50; // 同時也要設定超出部分不讓顯示 這樣就可以達到一個圓形的片- imageView.clipsToBounds = YES; [mainView.view addSubview:imageView];
再來看看layer層還帶有什麼屬性,可以設定什麼效果呢,一個是可以設定控制項的邊框大小以及顏色,有時候可以方便我們對項目集合控制項的測試說白了也就是更加清晰顯示出控制項的位置把比如說tableView 的cell 設定後可以很清楚看到每個cell的大小範圍,其次還可以實現點擊讓控制項有選中的效果,更加進階點就是通過layer層對控制項進行動畫的繪製
下面是蘋果API
@property CGFloat cornerRadius; //設定邊角半徑/* The width of the layer's border, inset from the layer bounds. The * border is composited above the layer's content and sublayers and * includes the effects of the `cornerRadius' property. Defaults to * zero. Animatable. */@property CGFloat borderWidth; // 設定控制項的邊框寬度/* The color of the layer's border. Defaults to opaque black. Colors * created from tiled patterns are supported. Animatable. */@property CGColorRef borderColor; // 設定控制項的邊框顏色/* The opacity of the layer, as a value between zero and one. Defaults * to one. Specifying a value outside the [0,1] range will give undefined * results. Animatable. */@property float opacity; // 設定控制項的透明度有部分動畫會用到/* When true, and the layer's opacity property is less than one, the * layer is allowed to composite itself as a group separate from its * parent. This gives the correct results when the layer contains * multiple opaque components, but may reduce performance. * * The default value of the property is read from the boolean * UIViewGroupOpacity property in the main bundle's Info.plist. If no * value is found in the Info.plist the default value is YES for * applications linked against the iOS 7 SDK or later and NO for * applications linked against an earlier SDK. */
// 只需要這兩句話就可以設定完成 imageView.layer.borderWidth = 2; // float類型資料 imageView.layer.borderColor = [UIColor redColor].CGColor; // 顏色需要是CGColor
ios之如何讓圖片顯示成圓形的樣式/設定控制項邊框大小以及顏色