CALayer 简单使用

2016-06-24  本文已影响151人  人生路02

片头曲 曲曲曲曲曲曲曲曲曲曲曲曲曲

CALayer

0.calyer的简介

1.使用calyer

layerView上添加一个蓝色layer

#import "ViewController.h"
#import
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIView *layerView;
@end
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    //create sublayer
    CALayer *blueLayer = [CALayer layer];
    blueLayer.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
    blueLayer.backgroundColor = [UIColor blueColor].CGColor;
    //add it to our view
    [self.layerView.layer addSublayer:blueLayer];
} @end

2.contents属性

用layer显示一张图片

@implementation ViewController
- (void)viewDidLoad
{
  [super viewDidLoad]; //load an image
  UIImage *image = [UIImage imageNamed:@"Snowman.png"];
  //add it directly to our view's layer
  self.layerView.layer.contents = (__bridge id)image.CGImage;
}
@end

3.contentGravity

CALayer 与 contentMode 相对应的属性叫 contentsGravity 但他是NSString类型,并不是枚举。

kCAGravityCenter 
kCAGravityTop 
kCAGravityBottom
kCAGravityLeft
kCAGravityRight
kCAGravityTopLeft
kCAGravityTopRight
kCAGravityBottomLeft
kCAGravityBottomRight
kCAGravityResize
kCAGravityResizeAspect 等同于 UIViewContentModeScaleAspectFit
kCAGravityResizeAspectFill

self.layerView.layer.contentsGravity = kCAGravityResizeAspect;

4.contentsScale

contentsScale 属性定义了寄宿图的像素尺寸和视图大小的比例,默认为1.0f.这个属性和屏幕分辨率有关系。如果为2.0f,那么就是Retina屏幕。这并不会对我们在使用kCAGravityResizeAspect时产生响因为他就是拉伸后的图片,已经适应图层。根本不考虑分辨率问题。kCAGravityCenter 不会拉伸图片。

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad]; //load an image
    UIImage *image = [UIImage imageNamed:@"Snowman.png"]; //add     it directly to our view's layer
    self.layerView.layer.contents = (__bridge   id)image.CGImage; //center the image
    self.layerView.layer.contentsGravity = kCAGravityCenter;
    //set the contentsScale to match image
    self.layerView.layer.contentsScale = image.scale;
}
@end
layer.contentsScale = [UIScreen mainScreen].scale;

5.maskToBounds

UIView有一个叫做clipsToBounds的属性可以用来决定是否显示超出边界的内容,CALayer对应的属性叫做masksToBounds,把它设置为YES,超出的部分就不会显示啦。

layer.masksToBounds = YES;

6.contentsRect

CALayer 的 contentsRect 允许我们在图层边框里显示寄宿图的一个子域。这会涉及到 contentsGravity 是如何显示和拉伸的。他的值为rect,值按单位计算[0,1]。

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addView:self.oneView rect:CGRectMake(0, 0, .5, .5)];
    [self addView:self.twoView rect:CGRectMake(.5, 0, .5, .5)];
    [self addView:self.threeView rect:CGRectMake(0, .5, .5, .5)];
    [self addView:self.fourView rect:CGRectMake(.5, .5, .5, .5)];
    
}

- (void)addView:(UIView *)view rect:(CGRect)rect{
    
    UIImage *img = [UIImage imageNamed:@"5"];
    
    view.layer.contents = (id)img.CGImage;
    view.layer.contentsGravity = kCAGravityResizeAspect;
    view.layer.contentsScale = [UIScreen mainScreen].scale;
    view.layer.contentsRect = rect;   
}

7.contentsCenter

contentsCenter 工作起来的效果和 UIImage 中 -resizableImageWithCapInsets:方法非常相似,只是它可以用到人和寄宿图,甚至包括Core Graphics。用图片rect区域来进行填充。保护边角不备拉伸。{0, 0, 1, 1}
可以在xib上设置,不用写代码。view -> srewtching

UIImage *imgzero = [UIImage imageNamed:@"44"];
    self.zeroView.layer.contents = (id)imgzero.CGImage;
//    self.zeroView.layer.contentsGravity = kCAGravityResizeAspect;
    self.zeroView.layer.contentsCenter = CGRectMake(0.5, 0.5, 0.1, 0.1);

8.Custom Drawing(自定义绘图)

给contents赋CGImage的值不是唯一的设置寄宿图的方法。我们也可以直接用Core Graphics直接绘制寄宿图。能够通过继承UIView并实现drawRect:方法来自定义绘制。
-drawRect: 方法没有默认的实现,因为对UIView来说,寄宿图并不是必须的,它不在意那到底是单调的颜色还是有一个图片的实例。如果UIView检测到-drawRect:方法被调用了,它就会为视图分配一个寄宿图,这个寄宿图的像素尺寸等于视图大小乘以contentsScale的值。

当需要被重绘时,CALayer会请求它的代理给他一个寄宿图来显示。它通过调用下面这个方法做到的:
(void)displayLayer:(CALayerCALayer *)layer;
趁着这个机会,如果代理想直接设置contents属性的话,它就可以这么做,不然没有别的方法可以调用了。如果代理不实现-displayLayer:方法,CALayer就会转而尝试调用下面这个方法:- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx;
在调用这个方法之前,CALayer创建了一个合适尺寸的空寄宿图(尺寸由bounds和contentsScale决定)和一个Core Graphics的绘制上下文环境,为绘制寄宿图做准备,他作为ctx参数传入。

@implementation ViewController
- (void)viewDidLoad
{
  [super viewDidLoad];
  
  //create sublayer
  CALayer *blueLayer = [CALayer layer];
  blueLayer.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
  blueLayer.backgroundColor = [UIColor blueColor].CGColor;

  //set controller as layer delegate
  blueLayer.delegate = self;

  //ensure that layer backing image uses correct scale
  blueLayer.contentsScale = [UIScreen mainScreen].scale; //add layer to our view
  [self.layerView.layer addSublayer:blueLayer];

  //force layer to redraw
  [blueLayer display];
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
  //draw a thick red circle
  CGContextSetLineWidth(ctx, 10.0f);
  CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
  CGContextStrokeEllipseInRect(ctx, layer.bounds);
}
@end
上一篇下一篇

猜你喜欢

热点阅读