1、通过设置CALayer来给视图控件添加阴影
#import "ViewController.h"
@interface ViewController ()
/**
* 测试View
*/
@property (nonatomic, strong) UIView *testView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.testView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
self.testView.backgroundColor = [UIColor orangeColor];
//0、圆角
self.testView.layer.cornerRadius = 5;
//1、设置图层阴影的范围
self.testView.layer.shadowRadius = 10;
//2、设置图层阴影的偏移量
self.testView.layer.shadowOffset = CGSizeMake(10, -10);
//3、设置图层阴影的颜色
self.testView.layer.shadowColor = [UIColor blackColor].CGColor;
//4、设置图层阴影的透明度
self.testView.layer.shadowOpacity = 0.7;
//5、添加边框宽度
self.testView.layer.borderWidth = 3.0;
//6、添加边框颜色
self.testView.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview:self.testView];
}
2、效果如下: