CAReplicatorLayer复制Layer和动画, 实现神奇的效果
生活随笔
收集整理的這篇文章主要介紹了
CAReplicatorLayer复制Layer和动画, 实现神奇的效果
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
今天我們看下CAReplicatorLayer,?官方的解釋是一個高效處理復(fù)制圖層的中間層。他能復(fù)制圖層的所有屬性,包括動畫。
一樣我們先看下頭文件
@interface CAReplicatorLayer : CALayer@property NSInteger instanceCount; //復(fù)制的個數(shù) @property BOOL preservesDepth; //這是一個bool值,默認(rèn)為No,如果設(shè)為Yes,將會具有3維透視效果 @property CFTimeInterval instanceDelay; //復(fù)制后的layer相比原來的距離 @property CATransform3D instanceTransform; //復(fù)制layer的坐標(biāo)系/方向偏轉(zhuǎn) @property(nullable) CGColorRef instanceColor;@property float instanceRedOffset; @property float instanceGreenOffset; @property float instanceBlueOffset; @property float instanceAlphaOffset;@end我們可以通過CAReplicatorLayer實(shí)現(xiàn)很炫的動畫, 比如這個
上代碼:
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];//創(chuàng)建一個紅色的圓形CALayerCALayer * layer = [CALayer layer];layer.bounds = CGRectMake(0, 0, 30, 30);layer.position = CGPointMake(self.view.center.x - 50, self.view.center.y - 50);layer.backgroundColor = [UIColor redColor].CGColor;layer.cornerRadius = 15;[self.view.layer addSublayer:layer];//創(chuàng)建一個透明度動畫CABasicAnimation * animation1 = [CABasicAnimation animationWithKeyPath:@"opacity"];animation1.fromValue = @(0);animation1.toValue = @(1);animation1.duration = 1.5;animation1.autoreverses = YES;//創(chuàng)建一個縮放動畫CABasicAnimation * animation2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];animation2.toValue = @(1.5);animation2.fromValue = @(0.5);animation2.duration = 1.5;animation2.autoreverses = YES;//創(chuàng)建一個動畫組, 將之前創(chuàng)建的透明度動畫和縮放動畫加入到這個動畫組中CAAnimationGroup * ani = [CAAnimationGroup animation];ani.animations = @[animation1,animation2];ani.duration = 1.5;ani.repeatCount = MAXFLOAT;ani.autoreverses = YES;//將動畫組添加到layer [layer addAnimation:ani forKey:nil];CAReplicatorLayer * rec = [CAReplicatorLayer layer];rec.instanceCount = 3;rec.instanceDelay = 0.5;rec.instanceTransform = CATransform3DMakeTranslation(50, 0, 0);[rec addSublayer:layer];[self.view.layer addSublayer:rec];CAReplicatorLayer * rec2 = [CAReplicatorLayer layer];rec2.instanceCount = 3;rec2.instanceDelay = 0.5;rec2.instanceTransform = CATransform3DMakeTranslation(0, 50, 0);[rec2 addSublayer:rec];[self.view.layer addSublayer:rec2]; }@end利用CAReplicatorLayer可以實(shí)現(xiàn)很多神奇的效果, 大家可以在發(fā)揮下腦洞
?
轉(zhuǎn)載于:https://www.cnblogs.com/zhouxihi/p/6296019.html
總結(jié)
以上是生活随笔為你收集整理的CAReplicatorLayer复制Layer和动画, 实现神奇的效果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: View绘制--onMeasure()
- 下一篇: 前端之DIV+CSS布局