UIView内部会自动创建一个图层就是CALayer对象,通过UIView的layer属性可以访问到这个层;当UIView需要显示到屏幕上,会调用drawRect: 方法进行绘图,并且会将所有内容绘制在自己的图层上,绘图完毕后,系统会将图层拷贝到屏幕上,于是就完成了UIView的显示;
- (void)myRedView
{
RedView *red = [[RedView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
red.backgroundColor = [UIColor whiteColor];
red.layer.cornerRadius = 50;
red.layer.borderWidth = 5;
red.layer.borderColor = [UIColor orangeColor].CGColor;
red.layer.contents = (id)[UIImage imageNamed:@"=0"].CGImage;
red.layer.masksToBounds = YES;
red.layer.shadowColor = [UIColor blackColor].CGColor;
red.layer.shadowOffset = CGSizeMake(20, 15);
red.layer.shadowOpacity = 0.5;
[self.view addSubview:red];
}
网友评论