程序员

iOS中Layer层的CABasicAnimation和CAAn

2017-03-01  本文已影响119人  2897275c8a00

   Copyright © 2017年ZaneWangWang. All rights reserved.

      如果你看到的不是原文请看原文iOS中Layer层CABasicAnimation和CAAnimationGroup应用。这篇文章主要介绍CABasicAnimation和CAAnimationGroup的混合使用作出相应效果,关于基础知识请看CoreAnimation开发框架中Layer层动画

先展示两种效果如下图:

类似雷达扫描效果的波纹图 常见下拉加载动画三点循环大小变化

知识不在赘述,直接上代码:

1.创建一个AnimationLayer类

.h文件如下

#import <QuartzCore/QuartzCore.h>

#import <UIKit/UIKit.h>

typedefNS_ENUM(NSInteger ,AnimationType){

LoadDataType =0,

SingleType

};

@interfaceAnimationLayer :CALayer

+(instancetype)layerWithType:(AnimationType)type;

@end

.m文件如下

#import"AnimationLayer.h"

@implementationAnimationLayer

+(instancetype)layerWithType:(AnimationType)type{

[superlayer];

AnimationLayer*contentLayer = [selflayer];

switch(type) {

caseLoadDataType:

[contentLayerloadDataAnimationWithContentLayer:contentLayer];

break;

caseSingleType:

[contentLayersingleAnimationWithContentLayer:contentLayer];

break;

default:

break;

}

returncontentLayer;

}

- (void)singleAnimationWithContentLayer:(AnimationLayer*)layer{

//self.view.layer.backgroundColor = [UIColor clearColor].CGColor;

CAShapeLayer*pulseLayer = [CAShapeLayerlayer];

pulseLayer.frame=CGRectMake(0,0,180,180);

//pulseLayer.

pulseLayer.path= [UIBezierPathbezierPathWithOvalInRect:pulseLayer.bounds].CGPath;

pulseLayer.fillColor= [UIColorgreenColor].CGColor;

pulseLayer.opacity=0.0;

CAReplicatorLayer*replicatorLayer = [CAReplicatorLayerlayer];

replicatorLayer.frame= layer.bounds;

replicatorLayer.instanceCount=8;

replicatorLayer.instanceDelay=0.5;

[replicatorLayeraddSublayer:pulseLayer];

[layeraddSublayer:replicatorLayer];

CABasicAnimation*opacityAnima = [CABasicAnimationanimationWithKeyPath:@"opacity"];

opacityAnima.fromValue=@(0.3);

opacityAnima.toValue=@(0.02);

CABasicAnimation*scaleAnima = [CABasicAnimationanimationWithKeyPath:@"transform"];

scaleAnima.fromValue= [NSValuevalueWithCATransform3D:CATransform3DScale(CATransform3DIdentity,0.0,0.0,0.0)];

scaleAnima.toValue= [NSValuevalueWithCATransform3D:CATransform3DScale(CATransform3DIdentity,1.0,1.0,0.0)];

CAAnimationGroup*groupAnima = [CAAnimationGroupanimation];

groupAnima.animations=@[opacityAnima, scaleAnima];

groupAnima.duration=3.0;

groupAnima.autoreverses=NO;

groupAnima.repeatCount=HUGE;

[pulseLayeraddAnimation:groupAnimaforKey:@"groupAnimation"];

}

- (void)loadDataAnimationWithContentLayer:(AnimationLayer*)layer{

CGFloatmargin =5.0f;//间隔

CGFloatcols =3.0f;//个数

CGFloatwidth = [UIScreenmainScreen].bounds.size.width/5.0f;//总体宽度

CGFloatdotW = (width - margin * (cols -1.0f)) / cols;//单个宽度

CAShapeLayer*dotLayer = [CAShapeLayerlayer];//一个layer

dotLayer.frame=CGRectMake(0,0, dotW, dotW);

dotLayer.path= [UIBezierPathbezierPathWithOvalInRect:CGRectMake(0,0, dotW, dotW)].CGPath;

dotLayer.fillColor= [UIColorredColor].CGColor;

CAReplicatorLayer*replicatorLayerX = [CAReplicatorLayerlayer];

replicatorLayerX.frame=CGRectMake(0,0, width, width);

replicatorLayerX.instanceDelay=0.3;

replicatorLayerX.instanceCount= cols;

[replicatorLayerXaddSublayer:dotLayer];

CATransform3Dtransform =CATransform3DTranslate(CATransform3DIdentity, dotW + margin,0,0);

replicatorLayerX.instanceTransform= transform;

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

CAReplicatorLayer*replicatorLayerY = [CAReplicatorLayerlayer];

replicatorLayerY.frame=CGRectMake(0,0, width, width);

replicatorLayerY.instanceDelay=0.3;

replicatorLayerY.instanceCount=3;

[replicatorLayerYaddSublayer:replicatorLayerX];

CATransform3DtransforY =CATransform3DTranslate(CATransform3DIdentity,0,0, dotW + margin);

replicatorLayerY.instanceTransform= transforY;

//添加动画

CABasicAnimation*opacityAnima = [CABasicAnimationanimationWithKeyPath:@"opacity"];

opacityAnima.fromValue=@(0.3);

opacityAnima.toValue=@(0.3);

CABasicAnimation*scaleAnima = [CABasicAnimationanimationWithKeyPath:@"transform"];

scaleAnima.fromValue= [NSValuevalueWithCATransform3D:CATransform3DScale(CATransform3DIdentity,1.0,1.0,0.0)];

scaleAnima.toValue= [NSValuevalueWithCATransform3D:CATransform3DScale(CATransform3DIdentity,0.2,0.2,0.0)];

CAAnimationGroup*groupAnima = [CAAnimationGroupanimation];

groupAnima.animations=@[opacityAnima, scaleAnima];

groupAnima.duration=1.0;

groupAnima.autoreverses=YES;

groupAnima.repeatCount=HUGE;

[dotLayeraddAnimation:groupAnimaforKey:@"groupAnimation"];

[layeraddSublayer:replicatorLayerY];

}

@end

2.在视图控制器中的使用如下图:

上一篇下一篇

猜你喜欢

热点阅读