美文网首页
iOS中的 呈现/模型 图层

iOS中的 呈现/模型 图层

作者: 大成小栈 | 来源:发表于2021-08-20 21:16 被阅读0次

图层树通常都是指图层树模型。在iOS中,屏幕每秒60帧,如果动画时长大于1帧的时长,CoreAnimation就需要在设置一次新值及其生效时间,来对屏幕上的图层进行重新组织。那么,Core Animation是如何判断出layer当前位置以及将要到达的位置的呢?

每个图层属性的显示值都被存储在一个presentationLayer的独立图层当中,它实际上是模型图层的复制,但是它的实时值代表了在任何时刻这个layer的外观效果。

呈现图层仅仅当图层首次被提交,即第一次在屏幕上显示的时候创建,所以在那之前调用presentationLayer将返回nil。

动画发生时,原layer将会被隐藏,动画停止后才生效。另一个图层属性modelLayer即可以被看作是原layer。通常在一个图层上调用modelLayer会返回self(已经创建的原始图层就是一种数据模型)。

下面的代码,可以帮助我们更好的了解呈现/模型图层:

@interface ViewController ()

@property(nonatomic,strong)CALayer*layerFirst;
@property(nonatomic,strong)CALayer*layerSecond;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor  orangeColor];

    self.layerSecond=[CALayer layer];
    self.layerSecond.frame=CGRectMake(0, 0, 40, 40);
    self.layerSecond.position=CGPointMake(30, 64+30);
    self.layerSecond.backgroundColor=[UIColor greenColor].CGColor;
    [self.view.layer addSublayer:self.layerSecond];
    
    self.layerFirst=[CALayer layer];
    self.layerFirst.frame = CGRectMake(0, 0, 100, 100);
    self.layerFirst.position = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height /2);
    self.layerFirst.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:self.layerFirst];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    CGPoint point = [[touches anyObject] locationInView:self.view];
    
    if ([self.layerFirst.presentationLayer hitTest:point]) {
        CGFloat red = arc4random() / (CGFloat)INT_MAX;
        CGFloat green = arc4random() / (CGFloat)INT_MAX;
        CGFloat blue = arc4random() / (CGFloat)INT_MAX;
        self.layerFirst.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
   }
    else if ([self.layerSecond.modelLayer hitTest:point]) {
       CGFloat red = arc4random() / (CGFloat)INT_MAX;
       CGFloat green = arc4random() / (CGFloat)INT_MAX;
       CGFloat blue = arc4random() / (CGFloat)INT_MAX;
       self.layerSecond.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
   }
    else {
        [CATransaction begin];
        [CATransaction setAnimationDuration:6.0];
        self.layerFirst.position = point;
        CGPoint point1=point;
        point1.x=point.x+100;
        self.layerSecond.position=point1;
        [CATransaction commit];
   }
}

@end
点击屏幕不同的的位置,并观察layer颜色
  1. 在移动的过程中或不移动时,若点击layerFirst时,颜色均会立马变化;
  2. 而在移动的过程中点击layerSecond时,则颜色不会变化,不移动的时才会变化。

相关文章

  • iOS中的 呈现/模型 图层

    图层树通常都是指图层树模型。在iOS中,屏幕每秒60帧,如果动画时长大于1帧的时长,CoreAnimation就需...

  • iOS动画原理--模型树和呈现树

    模型树和呈现树 CoreAnimation作为一个复合引擎,将不同的视图层组合在屏幕中,并且存储在图层树中,向我们...

  • View

    Model View Controller (MVC) 模型视图控制器(MVC)中的视图层,用于负责定义和呈现用户...

  • iOS动画事物(CATransaction)

    动画事物 CATransaction是 Core Animation 中的事务类,在iOS中的图层中,图层的每个改...

  • Core Animation系列之CATransaction

    CATransaction是 Core Animation 中的事务类,在iOS中的图层中,图层的每个改变都是事务...

  • iOS中模型树和呈现树

    整个过程其实经历了三个树状结构,才显示到了屏幕上:模型树-->呈现树-->渲染树 通常,我们操作的是模型树,在重绘...

  • UITableView视图层级的变化

    UITableView视图层级的变化 iOS7 UITableView的视图层级变化 在iOS的开发过程中,我们经...

  • ArcGlobe之浮动图层

    在ArcGlobe中,有叠加图层、高程图层和浮动图层,前两个分别是准备呈现的数据和所使用的地形,而浮动图层是什么呢...

  • iOS性能优化篇

    iOS页面图层性能和页面卡顿的原因 iOS系统如何把图像内容展示到屏幕,呈现到我们的面前?为什么APP会感觉卡顿?...

  • CALayer

    初探CALayer属性 IOS中CALayer的使用//这个算是比较全了 iOS - CALayer 绘图层 iO...

网友评论

      本文标题:iOS中的 呈现/模型 图层

      本文链接:https://www.haomeiwen.com/subject/yscxiltx.html