UIView,UIButton,UIImageView等設置圓角,設置陰影,設置邊框的方法app
在iOS開發中,任何可見視圖都是繼承於UIView的。 繼承體系中,大部分UIView的屬性適用於其任何孩子。動畫
而UIView的layer屬性能夠繪製UIView的各類效果。其實咱們看到的View的動畫實際上也是layer在繪製。spa
cornerView.layer.cornerRadius = 20; cornerView.layer.masksToBounds = YES;
masksToBounds防止子元素溢出父視圖。code
若是一個正方形要設置成圓形,代碼爲:繼承
cornerView.layer.cornerRadius = cornerView.frame.size.height/2; cornerView.layer.masksToBounds = YES;
borderView.layer.borderWidth = 1.0; borderView.layer.borderColor = [UIColor blackColor].CGColor;
注意此處使用的是CGColor而不是UIColor.ip
shadowView.layer.shadowColor = [UIColor redColor].CGColor; shadowView.layer.shadowOffset = CGSizeMake(5.0, 5.0); shadowView.layer.shadowOpacity = YES;
offset爲偏移量,爲正表示向frame x,y座標增長的方向偏移。ci
opacity爲透明度,默認爲0,即表示透明的。因此咱們要把opacity設置成1或者YES,表示不透明,也能夠設置成0.5或者相似的值呈現半透明。開發
效果以下:get
UIView *v=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
v.backgroundColor=[UIColor yellowColor];
//v.layer.masksToBounds=YES;這行去掉
v.layer.cornerRadius=10;
v.layer.shadowColor=[UIColor redColor].CGColor;
v.layer.shadowOffset=CGSizeMake(10, 10);
v.layer.shadowOpacity=0.5;
v.layer.shadowRadius=5;it
[self.view addSubview:v];
效果以下
/* When true an implicit mask matching the layer bounds is applied to * the layer (including the effects of the `cornerRadius' property). If * both `mask' and `masksToBounds' are non-nil the two masks are * multiplied to get the actual mask values. Defaults to NO. * Animatable. */