Core Animation实战一(认识图层CALayer)
程序员文章站
2022-03-16 17:11:10
...
前言:
本文主要是苹果官网文档Core Animation例子的总结学习,主要是实战代码,不过多BB概念,几年的学习经验总结出,通过代码分析概念对我来说更有兴趣,更好吸收。
Core Animation其实是一个令人误解的命名。你可能认为它只是用来做动画的,但实际上它是从一个叫做Layer Kit这么一个不怎么和动画有关的名字演变而来,所以做动画这只是Core Animation特性的冰山一角。
Core Animation是一个复合引擎,它的职责就是尽可能快地组合屏幕上不同的可视内容,这个内容是被分解成独立的图层,存储在一个叫做图层树的体系之中。于是这个树形成了UIKit以及在iOS应用程序当中你所能在屏幕上看见的一切的基础。
认识CALayer
创建Layer和Layer的基本几个简单的属性,不好说明的以后会单独拿出来做例子。//
// ViewController.m
// LayerStudyDemo
//
// Created by apple on 2017/9/15.
// Copyright © 2017年 ZY. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
CALayer * yellowLayer;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
//是否沿着Y轴翻转
self.view.layer.geometryFlipped = YES;
[self layerDemo];
}
//创建一个图层
-(void)layerDemo{
yellowLayer = [CALayer layer];
yellowLayer.frame = CGRectMake(50, 200, 100, 100);
yellowLayer.backgroundColor = [UIColor yellowColor].CGColor;
//设置圆角
yellowLayer.cornerRadius = 30;
//默认四个圆角,你也可以选择圆角个数
yellowLayer.maskedCorners = kCALayerMinXMaxYCorner|kCALayerMinXMinYCorner;
//BOOL,Animatable。图层有双面,是否都显示,设置NO意思背面看不到。下图是两个图层分别设置doubleSided为NO和YES翻转180°的效果。默认值为YES
yellowLayer.doubleSided = NO;
CATextLayer * textLayer = [CATextLayer layer];
textLayer.frame = yellowLayer.bounds;
textLayer.string = @"我们不一样";
[yellowLayer addSublayer:textLayer];
CALayer * blueLayer = [CALayer layer];
blueLayer.frame = CGRectMake(50, 200, 100, 100);
blueLayer.backgroundColor = [UIColor blueColor].CGColor;
blueLayer.anchorPoint = CGPointMake(0.0f, 0.0f);
[self.view.layer addSublayer:blueLayer];
[self.view.layer addSublayer:yellowLayer];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
上一篇: 节流的详细解读
下一篇: React入门(一):Why React