iOS layer的理解

1. Layer层锚点座标的理解

    viewOne.layer.position = CGPointMake( 200.f, 200.f);

    viewOne.layer.anchorPoint = CGPointMake(0.5f, 0.5f); //默认值为0.5  0.5

    viewOne.layer.bounds = CGRectMake(0.f, 0.f, 100.f, 100.f);

   ///position {200, 200} anchorPoint {1, 0}

    ///position {200, 200} anchorPoint {0.5, 0.5}

    ///position {200, 200} anchorPoint {0, 1}

    ///position {200, 200} anchorPoint {1, 1}    

NSLog(@"position:%@,anchorPoint:%@",NSStringFromCGPoint(viewOne.layer.position),NSStringFromCGPoint(viewOne.layer.anchorPoint));

结论:1.  锚点和position这个点  是同一个点

            2.  锚点是这个点相对于layer的座标, postion是这个点相对于父view 的座标

            3.  锚点的取值范围(0~1)

            4. 旋转/变换一个view 的时候, 是围绕着锚点来做的旋转和变换 

   应用: 时钟的旋转

//我们的layer或者view,你对它进行旋转的时候,它是围绕着锚点来旋转的
    //我写一个秒针,我让秒针来动
    CALayer *secondLayer = [CALayer layer];
    secondLayer.bounds = CGRectMake(0.f, 0.f, 10.f, 40.f);
    secondLayer.position = CGPointMake(50.f, 50.f);
    secondLayer.anchorPoint = CGPointMake(0.5f, 0.8f);
    secondLayer.backgroundColor = [UIColor redColor].CGColor;
    [viewTwo.layer addSublayer:secondLayer];
    
    static int number = 0;
    
    [NSTimer scheduledTimerWithTimeInterval:1.f repeats:YES block:^(NSTimer * _Nonnull timer) {
       
        number++;
        
        secondLayer.transform = CATransform3DMakeRotation(number*2*M_PI/60, 0, 0, 1);
        
    }];

2.  layContent

      view有一个layer属性, layer 的代理默认是view,layer内容的展示有两个代理方法

///layer内容展示的两个代理方法
- (void)displayLayer:(CALayer *)layer {

     NSLog(@"%s", __func__);
    layer.contents = (__bridge id)[UIImage imageNamed:@"1.png"].CGImage;

}


- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {

    NSLog(@"%s", __func__);
    [super drawLayer:layer inContext:ctx];

}

3. 模型树和呈现树

       layer层有三层树结构:presentationLayer  tree(呈现树)、modelLayer tree(模型树)、Render tree (渲染树)

       模型树:用来存储数据的、响应事件

       呈现树:用来根据模型树的数据,展示

       呈现树是对模型树的copy

如果添加了一个动画,此时成现实的数据来源于动画,而不是模型数据,当动画结束之后,才又会根据模型树的数据展示,模型树的数据来源于动画。

引申:每一个runloop循环的时候, 会提交一个事物:CATransaction,让呈现树根据数据展示。

4. 通过mask获得不规则形状

imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100.f, 100.f, 200.f, 200.f)];
    imgView.image = [UIImage imageNamed:@"bg-mine.png"];
    [self.view addSubview:imgView];
    
    //通过layer.mask 来塑造一个不透明控件
    UIImage *image = [UIImage imageNamed:@"bubble.png"];
    CALayer *imageLayer = [CALayer layer];
    imageLayer.contents = (__bridge id)image.CGImage;
    imageLayer.frame = imgView.bounds;
    imgView.layer.mask = imageLayer;

5. layer隐式动画

/**

 

动画的流程:有delegate,执行delegate,没有的话寻找actions里的action;然后再寻找style里的action;defaultActionForKey;调用actionForKey,如果有action返回,那么执行addAnimationForKey方法;

//上述流程,如果返回为nil,继续走流程,为非nil,就停止

 

* 1. if defined, call the delegate method -actionForLayer:forKey:

* 2. look in the layer's `actions' dictionary

* 3. look in any `actions' dictionaries in the `style' hierarchy

* 4. call +defaultActionForKey: on the layer's class

*/

- (nullable id<CAAction>)actionForKey:(NSString *)event {

    

    NSLog(@"%s", __func__);

    

    NSLog(@"%@", [super actionForKey:event]);

    

    return [super actionForKey:event];

    

}



+ (id<CAAction>)defaultActionForKey:(NSString *)event {

    

    NSLog(@"%s", __func__);

    

    return [super defaultActionForKey:event];

    

}



- (void)addAnimation:(CAAnimation *)anim forKey:(nullable NSString *)key {

    

    NSLog(@"%s", __func__);

    [super addAnimation:anim forKey:key];

    

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章