//一般大家都会这样设置 你会发现投影效果并没有呈现出来
UIView *samall = [[UIViewalloc]initWithFrame:CGRectMake(100,350,100,100)];
samall.backgroundColor = [UIColorredColor];
samall.layer.cornerRadius =10;
samall.layer.masksToBounds =YES;
[self.viewaddSubview:samall];
samall.layer.shadowOffset =CGSizeMake(5,5);
samall.layer.shadowColor = [UIColorlightGrayColor].CGColor;
samall.layer.shadowRadius =10;
samall.layer.shadowOpacity =1;
samall.layer.cornerRadius =10;
//原因是因为
clipsToBounds 指的是视图上的子视图,如果超出父视图部分就会被裁剪掉
masksToBounds 指的是视图图层上的子视图,如果超出父图层部分就会被裁剪掉
试一试下边的方法吧
方法1
UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(100,100,100,100)];
view.backgroundColor = [UIColorredColor];
view.layer.cornerRadius =10;
view.layer.masksToBounds=YES;
UIView *bgShadowView = [[UIViewalloc]initWithFrame:view.frame];
[self.viewaddSubview:bgShadowView];
bgShadowView.layer.shadowColor = [UIColorlightGrayColor].CGColor;
bgShadowView.layer.shadowOffset =CGSizeMake(5,5);
bgShadowView.layer.shadowOpacity =1;
bgShadowView.layer.shadowRadius =9.0;
bgShadowView.layer.cornerRadius =9.0;
bgShadowView.clipsToBounds =NO;
[bgShadowView addSubview:view];
方法2
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(50, 100, 200,100)];
view.backgroundColor = LColor(250, 88, 88);
[self.view addSubview:view];
view.layer.cornerRadius = 20;
view.layer.masksToBounds = NO;
view.layer.shadowOffset = CGSizeMake(1, 5);
view.layer.shadowRadius = 5;
view.layer.shadowColor = [UIColor grayColor].CGColor;
view.layer.shadowOpacity = 0.9;