iOS小技巧--UIView,UIButton,UIImageView等设置圆角,设置阴影,设置边框

UIView,UIButton,UIImageView等设置圆角,设置阴影,设置边框的方法

    在iOS开发中,任何可见视图都是继承于UIView的。    继承体系中,大部分UIView的属性适用于其任何孩子。

UIView,UIButton,UIImageView等设立圆角,设置阴影,设置边框的方法

UIView,UIButton,UIImageView等设立圆角,设置阴影,设置边框的方法

UIView,UIButton,UIImageView等设立圆角,设置阴影,设置边框的方法

UIView,UIButton,UIImageView等设立圆角,设置阴影,设置边框的方法

       而UIView的layer属性可以绘制UIView的各种效果。其实我们看到的View的动画实际上也是layer在绘制。

 

1、绘制圆角

 

    cornerView.layer.cornerRadius = 20;
    cornerView.layer.masksToBounds = YES;

masksToBounds防止子元素溢出父视图。

如果一个正方形要设置成圆形,代码为:

 

    cornerView.layer.cornerRadius = cornerView.frame.size.height/2;
    cornerView.layer.masksToBounds = YES;

 

 

2、绘制边框

 

    borderView.layer.borderWidth = 1.0;
    borderView.layer.borderColor = [UIColor blackColor].CGColor;


注意此处使用的是CGColor而不是UIColor.

 

3、绘制阴影

 

    shadowView.layer.shadowColor = [UIColor redColor].CGColor;
    shadowView.layer.shadowOffset = CGSizeMake(5.0, 5.0);
    shadowView.layer.shadowOpacity = YES;


offset为偏移量,为正表示向frame x,y坐标增加的方向偏移。

opacity为透明度,默认为0,即表示透明的。所以我们要把opacity设置成1或者YES,表示不透明,也可以设置成0.5或者类似的值呈现半透明。

 

效果如下:

 

225732_uLL4_2331935.png

 

 

4、圆角阴影并存

 

  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;

    [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. */

转载于:https://my.oschina.net/u/2331935/blog/403628

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值