ios之如何讓圖片顯示成圓形的樣式/設定控制項邊框大小以及顏色

來源:互聯網
上載者:User

標籤: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之如何讓圖片顯示成圓形的樣式/設定控制項邊框大小以及顏色

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.