关注iOS Developer

iOS-Ui-常用核心动画分享

2016-10-14  本文已影响70人  iOSWjd

1. 常见的位移动画效果实现

- (void)positionAction

{

CABasicAnimation*posAnim = [CABasicAnimationanimationWithKeyPath:@"position"];

// 起始坐标点位置

CGPointstartP =CGPointMake(88,88);

// 终点坐标点位置

CGPointendP =CGPointMake(10,333);

//从某个点开始

posAnim.fromValue= [NSValuevalueWithCGPoint:startP];

//到某个点结束

posAnim.toValue= [NSValuevalueWithCGPoint:endP];

//多少时间内完成这个动画

posAnim.duration=2;

//保持当前最新的位置

posAnim.fillMode=kCAFillModeForwards;

//在完成的时候,保留动画

posAnim.removedOnCompletion=NO;

//原路径返回

posAnim.autoreverses=YES;

//添加到myView上

[self.myView.layeraddAnimation:posAnimforKey:@"11"];

}


2. 翻转,类似于电子书软件的翻页效果

- (void)CATransionAction

{

self.myView.image= [UIImageimageNamed:@“YztciOS"];

CATransition*tranAnim = [CATransitionanimation];

tranAnim.type=kCATransitionMoveIn;

tranAnim.subtype=kCATransitionFromRight;

[self.myView.layeraddAnimation:tranAnimforKey:nil];

}


3. 抖动,类似于iOS系统长按删除App的效果

- (void)appDeleteAct

{

CAKeyframeAnimation*anim = [CAKeyframeAnimationanimationWithKeyPath:@"transform.rotation.z"];

CGFloatleftF =M_PI_4/45*10;

CGFloatrightF =M_PI_4/45* -10;

//向左转动10°,再向右转动10°,再向左转动10°

anim.values=@[@(leftF),@(rightF),@(leftF)];

//单位时间内

anim.duration=1;

//转动次数

anim.repeatCount=MAXFLOAT;

[self.myView.layeraddAnimation:animforKey:nil];

}


4. 缩放效果

- (void)scaleAnimation

{

CABasicAnimation*anim = [CABasicAnimationanimationWithKeyPath:@"transform.scale"];

anim.fromValue=@1;

anim.toValue=@.3;

anim.repeatCount=MAXFLOAT;

anim.duration=.8;

anim.autoreverses=YES;

//控制动画单位时间内的速度

anim.timingFunction= [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn];

[self.myView.layeraddAnimation:animforKey:nil];

}


5. 转动效果

- (void)tranAnimation

{

//删除myView.layer上叫做tranfName的动画

[self.myView.layerremoveAnimationForKey:@"tranfName"];

CABasicAnimation*tranAnim = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.z"];

//旋转角度

tranAnim.toValue= @M_PI_4;

//动画时间

tranAnim.duration=0.5;

//转动次数

tranAnim.repeatCount=MAXFLOAT;

//累加

tranAnim.cumulative=YES;

//添加动画

[self.myView.layeraddAnimation:tranAnimforKey:@"tranfName"];

}


6. 透明度渐变效果

- (void)opacityAct

{

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

anim.fromValue=@1;

anim.toValue=@0;

anim.duration=1;

anim.repeatCount=MAXFLOAT;

anim.autoreverses=YES;

[self.myView.layeraddAnimation:animforKey:nil];

}


7. 多个坐标点的运动轨迹效果,用贝瑟尔曲线进行实现

- (void)keyfromPathAction

{

CAKeyframeAnimation*pathAnim = [CAKeyframeAnimationanimationWithKeyPath:@"position"];

UIBezierPath*path = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(99,99,200,200)];

//将路径赋值给动画

pathAnim.path= path.CGPath;

//时间间隔

pathAnim.duration=5;

pathAnim.repeatCount=MAXFLOAT;

[self.myView.layeraddAnimation:pathAnimforKey:nil];

}


8. 多个坐标点的运动轨迹效果,用Value进行实现

- (void)muchPoint

{

CAKeyframeAnimation*anim = [CAKeyframeAnimationanimationWithKeyPath:@"position"];

NSValue*v1 = [NSValuevalueWithCGPoint:CGPointMake(88,88)];

NSValue*v2 = [NSValuevalueWithCGPoint:CGPointMake(188,188)];

NSValue*v3 = [NSValuevalueWithCGPoint:CGPointMake(4,88)];

NSValue*v4 = [NSValuevalueWithCGPoint:CGPointMake(88,2)];

NSValue*v5 = [NSValuevalueWithCGPoint:CGPointMake(0,488)];

NSValue*v6 = [NSValuevalueWithCGPoint:CGPointMake(288,388)];

anim.values=@[v1,v2,v3,v4,v5,v6];

anim.duration=5;

//让轨迹保存在最终位置

anim.fillMode=kCAFillModeForwards;

//在动画完成后,保留动画

anim.removedOnCompletion=NO;

//原路径返回

anim.autoreverses=YES;

//添加核心动画

[self.myView.layeraddAnimation:animforKey:nil];

}


9. 多个核心动画以组合的方式同时实现(常用,核心动画重点,必须熟练掌握)

- (void)groupAnim

{

[self.view.layerremoveAllAnimations];

//跳动效果

CABasicAnimation*scaleAnim = [CABasicAnimationanimationWithKeyPath:@"transform.scale"];

scaleAnim.fromValue=@2;

scaleAnim.toValue=@.4;

//旋转效果

CABasicAnimation*tranAnim = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.z"];

tranAnim.toValue= @M_PI_4;

//闪烁效果

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

opacityAnim.fromValue=@1;

opacityAnim.toValue=@0;

//多点运动轨迹(path)

CAKeyframeAnimation*posAnim = [CAKeyframeAnimationanimationWithKeyPath:@"position"];

UIBezierPath*path = [UIBezierPathbezierPath];

[pathmoveToPoint:CGPointMake(99,99)];

[pathaddLineToPoint:CGPointMake(11,11)];

[pathaddLineToPoint:CGPointMake(199,399)];

[pathclosePath];

posAnim.path= path.CGPath;

//动画组合效果

CAAnimationGroup*groupAnim = [CAAnimationGroupanimation];

groupAnim.animations=@[scaleAnim,tranAnim,opacityAnim,posAnim];

groupAnim.duration=5;

groupAnim.fillMode=kCAFillModeForwards;

groupAnim.removedOnCompletion=NO;

[self.myView.layeraddAnimation:groupAnimforKey:nil];

}

上一篇 下一篇

猜你喜欢

热点阅读