iOS-设置阴影和圆角效果

参考链接:视觉效果和变换

iOS 给一个设置过圆角的View添加阴影效果

1.圆角效果

wechatBt.layer.cornerRadius = 8;
wechatBt.layer.masksToBounds = YES;//超出父视图部分不显示。


2.阴影效果

messageBt.layer.shadowOffset = CGSizeMake(1, 1);
messageBt.layer.shadowOpacity = 0.8;
messageBt.layer.shadowColor = [UIColor blackColor].CGColor;


3.圆角加阴影效果(此方法错误)

messageBt.layer.cornerRadius = 8;
//messageBt.layer.masksToBounds = NO;//此处不可以设置YES,否则阴影效果无法实现,但是设置为NO时,圆角无法实现。
messageBt.layer.shadowOffset = CGSizeMake(1, 1);
messageBt.layer.shadowOpacity = 0.8;
messageBt.layer.shadowColor = [UIColor blackColor].CGColor;

 

解决方法:

创建一个view,设置阴影效果,然后将所需视图添加到view中,设置圆角效果即可。

UIView *shadowView = [[UIView alloc]initWithFrame:CGRectMake(60, 60, 100, 100)];
    shadowView.backgroundColor = UIColor.blueColor;
    shadowView.userInteractionEnabled = YES;
    shadowView.layer.cornerRadius = 50;
    shadowView.layer.shadowOffset = CGSizeMake(1, 5);
    shadowView.layer.shadowOpacity = 0.8;
    shadowView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    //shadowView.layer.masksToBounds = NO;
    [self.view addSubview:shadowView];
    
    UIButton *messageBt = [UIButton buttonWithType:UIButtonTypeSystem];
    [messageBt setFrame:CGRectMake(0, 0, 100, 100)];
    [messageBt setBackgroundColor:UIColor.cyanColor];
    [messageBt setTitle:@"button" forState:UIControlStateNormal];
    messageBt.layer.cornerRadius = 50;
    messageBt.layer.masksToBounds = YES;
    [shadowView addSubview: messageBt];

Demo地址:https://github.com/cxymq/ShadowAndRadius

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值