美文网首页
Core Animation小记(二)

Core Animation小记(二)

作者: 凯旋之歌 | 来源:发表于2017-09-01 11:01 被阅读0次

    1.CAShapeLayer

    CAShapeLayer是一个通过矢量图形而不是bitmap来绘制的图层子类。你指定诸如颜色和线宽等属性,用CGPath来定义想要绘制的图形,最后CAShapeLayer就自动渲染出来了

    相比Core Graphics之下,使用CAShapeLayer有以下一些优点:

    渲染快速。CAShapeLayer使用了硬件加速,绘制同一图形会比用Core Graphics快很多。

    高效使用内存。一个CAShapeLayer不需要像普通CALayer一样创建一个寄宿图形,所以无论有多大,都不会占用太多的内存。

    不会被图层边界剪裁掉。一个CAShapeLayer可以在边界之外绘制。你的图层路径不会像在使用Core Graphics的普通CALayer一样被剪裁掉(如我们在第二章所见)。

    不会出现像素化。当你给CAShapeLayer做3D变换时,它不像一个有寄宿图的普通图层一样变得像素化。

    CAShapeLayer通过CGPath来表示形状。可以控制属性,lineWidth,lineCap,lineJoin,不过这些形状都只能是同一样的,一个layer只能设置一种。

    创建带圆角和直角的图,创建圆角矩形其实就是人工绘制单独的直线和弧度,但是事实上UIBezierPath有自动绘制圆角矩形的构造方法

    - (void)setCornerWithFlaot:(CGFloat)corner byRoundingCorners:(UIRectCorner)corners{

    //define path

    CGRect rect = self.bounds;

    CGSize radii = CGSizeMake(corner, corner);

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect

    byRoundingCorners:corners

    cornerRadii:radii];

    //define layer

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

    shapeLayer.frame = self.bounds;

    shapeLayer.path = path.CGPath;

    self.layer.mask = shapeLayer;

    }

    2.CATransformLayer

    CATransformLayerk可以将CALayer的构造的3D图链接起来放入同一3D空间中

    3.CAGradientLayer

    CAGradientLayer是用来生成两种或更多颜色平滑渐变的。但是CAGradientLayer的真正好处在于绘制使用了硬件加速。这些渐变色彩放在一个数组中,并赋给colors属性

    - (void)viewDidLoad

    {

    [super viewDidLoad];

    //create gradient layer and add it to our container view

    CAGradientLayer *gradientLayer = [CAGradientLayer layer];

    gradientLayer.frame = self.containerView.bounds;

    [self.containerView.layer addSublayer:gradientLayer];

    //set gradient colors

    gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor blueColor].CGColor];

    //set gradient start and end points

    gradientLayer.startPoint = CGPointMake(0, 0);

    gradientLayer.endPoint = CGPointMake(1, 1);

    }

    默认情况下,这些颜色在空间上均匀地被渲染,但是我们可以用locations属性来调整空间。locations属性是一个浮点数值的数组(以NSNumber包装)。这些浮点数定义了colors属性中每个不同颜色的位置,同样的,也是以单位坐标系进行标定。0.0代表着渐变的开始,1.0代表着结束。

    //set locations

    gradientLayer.locations = @[@0.0, @0.25, @0.5];

    4.CAReplicatorLayer

    CAReplicatorLayer的目的是为了高效生成许多相似的图层。

    CAReplicatorLayer真正应用到实际程序上的场景比如:一个游戏中导弹的轨迹云,或者粒子爆炸(尽管iOS 5已经引入了CAEmitterLayer,它更适合创建任意的粒子效果)。除此之外,还有一个实际应用是:反射。

    指定一个继承于UIView的ReflectionView,它会自动产生内容的反射效果

    @implementation ReflectionView

    + (Class)layerClass

    {

    return [CAReplicatorLayer class];

    }

    - (void)setUp

    {

    //configure replicator

    CAReplicatorLayer *layer = (CAReplicatorLayer *)self.layer;

    layer.instanceCount = 2;

    //move reflection instance below original and flip vertically

    CATransform3D transform = CATransform3DIdentity;

    CGFloat verticalOffset = self.bounds.size.height + 2;

    transform = CATransform3DTranslate(transform, 0, verticalOffset, 0);

    transform = CATransform3DScale(transform, 1, -1, 0);

    layer.instanceTransform = transform;

    //reduce alpha of reflection layer

    layer.instanceAlphaOffset = -0.6;

    }

    - (id)initWithFrame:(CGRect)frame

    {

    //this is called when view is created in code

    if ((self = [super initWithFrame:frame])) {

    [self setUp];

    }

    return self;

    }

    - (void)awakeFromNib

    {

    //this is called when view is created from a nib

    [self setUp];

    }

    @end

    5.CATiledLayer

    CATiledLayer很好地和UIScrollView集成在一起。除了设置图层和滑动视图边界以适配整个图片大小,我们真正要做的就是实现-drawLayer:inContext:方法,当需要载入新的小图时,CATiledLayer就会调用到这个方法。

    #import "ViewController.h"    

    @interface ViewController ()

    @property (nonatomic, weak) IBOutlet UIScrollView *scrollView;

    @end

    @implementation ViewController

    - (void)viewDidLoad

    {

    [super viewDidLoad];

    //add the tiled layer

    CATiledLayer *tileLayer = [CATiledLayer layer];

    tileLayer.frame = CGRectMake(0, 0, 2048, 2048);

    tileLayer.delegate = self;

    tileLayer.contentsScale = [UIScreen mainScreen].scale;

    [self.scrollView.layer addSublayer:tileLayer];

    //configure the scroll view

    self.scrollView.contentSize = tileLayer.frame.size;

    //draw layer

    [tileLayer setNeedsDisplay];

    }

    - (void)drawLayer:(CATiledLayer *)layer inContext:(CGContextRef)ctx

    {

    //determine tile coordinate

    CGRect bounds = CGContextGetClipBoundingBox(ctx);

    CGFloat scale = [UIScreen mainScreen].scale;

    NSInteger x = floor(bounds.origin.x / layer.tileSize.width* scale);

    NSInteger y = floor(bounds.origin.y / layer.tileSize.height* scale);

    //load tile image

    NSString *imageName = [NSString stringWithFormat: @"Snowman_%02i_%02i", x, y];

    NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"];

    UIImage *tileImage = [UIImage imageWithContentsOfFile:imagePath];

    //draw tile

    UIGraphicsPushContext(ctx);

    [tileImage drawInRect:bounds];

    UIGraphicsPopContext();

    }

    @end

    CAEmitterLayer

    苹果引入了一个新的CALayer子类叫做CAEmitterLayer。CAEmitterLayer是一个高性能的粒子引擎,被用来创建实时例子动画如:烟雾,火,雨等等这些效果。

    AVPlayerLayer

    AVPlayerLayer是用来在iOS上播放视频的。他是高级接口例如MPMoivePlayer的底层实现,提供了显示视频的底层控制。AVPlayerLayer的使用相当简单:你可以用+playerLayerWithPlayer:方法创建一个已经绑定了视频播放器的图层,或者你可以先创建一个图层,然后用player属性绑定一个AVPlayer实例。

    - (void)viewDidLoad

    {

    [super viewDidLoad];

    //get video URL

    NSURL *URL = [[NSBundle mainBundle] URLForResource:@"Ship" withExtension:@"mp4"];

    //create player and player layer

    AVPlayer *player = [AVPlayer playerWithURL:URL];

    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];

    //set player layer frame and attach it to our view

    playerLayer.frame = self.containerView.bounds;

    [self.containerView.layer addSublayer:playerLayer];

    //play the video

    [player play];

    }

    因为AVPlayerLayer是CALayer的子类,它继承了父类的所有特性。我们并不会受限于要在一个矩形中播放视频;清单6.16演示了在3D,圆角,有色边框,蒙板,阴影等效果(见图6.17).

    - (void)viewDidLoad

    {

    ...

    //set player layer frame and attach it to our view

    playerLayer.frame = self.containerView.bounds;

    [self.containerView.layer addSublayer:playerLayer];

    //transform layer

    CATransform3D transform = CATransform3DIdentity;

    transform.m34 = -1.0 / 500.0;

    transform = CATransform3DRotate(transform, M_PI_4, 1, 1, 0);

    playerLayer.transform = transform;

    //add rounded corners and border

    playerLayer.masksToBounds = YES;

    playerLayer.cornerRadius = 20.0;

    playerLayer.borderColor = [UIColor redColor].CGColor;

    playerLayer.borderWidth = 5.0;

    //play the video

    [player play];

    }

    相关文章

      网友评论

          本文标题:Core Animation小记(二)

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