您的位置:首页 > 其它

CALayer简单应用 —— 阴影

2015-12-22 17:16 253 查看
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong)
CALayer *imageLayer;
@property (nonatomic,strong)
UIImage *image;
@end

@implementation ViewController

- (void)viewDidLoad {
    [superviewDidLoad];
    self.image = [UIImageimageNamed:@"end.jpeg"];
    self.imageLayer = [CALayerlayer];

注:设置layer的背景图片,可作为图片的载体(此处类似ImageView)
    self.imageLayer.contents = (__bridgeid
_Nullable)(self.image.CGImage);
//    self.imageLayer.borderWidth = 1.0f;

//    self.imageLayer.borderColor = [UIColor cyanColor].CGColor;

注:通常我们设置layer的位置和大小时,用bounds和position来代替frame。 原因是frame不属于隐性动画,从而不会触发CABasicAnimation动画(如果不考虑动画效果可以用frame)。
    self.imageLayer.bounds =CGRectMake(0,0,
120,200);
    self.imageLayer.position =self.view.center;
    self.imageLayer.shadowColor = [UIColor blackColor].CGColor;

    self.imageLayer.shadowOffset = CGSizeMake(3.0f, 3.0f);

注:设置阴影时,一定要设置该属性(相当于view的alpha属性),系统默认为0,如果不设置,就不会显示阴影

    self.imageLayer.shadowOpacity = 0.8f;
    [self.view.layeraddSublayer:self.imageLayer];
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CALayer