该篇文章CABasicAnimation动画类的一些类型展示和使用说明。Talk is cheap.Show the code.
震动效果
-(void)shakeAnimationForView:(UIView *) view
{
// 获取到当前的View
CALayer *viewLayer = view.layer;
// 获取当前View的位置
CGPoint position = viewLayer.position;
// 移动的两个终点位置
CGPoint x = CGPointMake(position.x + 10, position.y);
CGPoint y = CGPointMake(position.x - 10, position.y);
// 设置动画
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
// 设置运动形式
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
// 设置开始位置
[animation setFromValue:[NSValue valueWithCGPoint:x]];
// 设置结束位置
[animation setToValue:[NSValue valueWithCGPoint:y]];
// 设置自动反转
[animation setAutoreverses:YES];
// 设置时间
[animation setDuration:.06];
// 设置次数
[animation setRepeatCount:5];
// 添加上动画
[viewLayer addAnimation:animation forKey:nil];
}
透明效果
-(void)opacityForView:(UIView*)view
{
CALayer *viewLayer = view.layer;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
animation.fromValue=@0;
animation.toValue=@1;
[animation setAutoreverses:YES];
[animation setDuration:1.0];
[animation setRepeatCount:3];
[viewLayer addAnimation:animation forKey:nil];
}
翻转效果
-(void)widthTranfromForView:(UIView*)view
{
CALayer *viewLayer = view.layer;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
animation.fromValue=@0;
animation.toValue=@1;
[animation setAutoreverses:YES];
[animation setDuration:1.0];
[animation setRepeatCount:3];
[viewLayer addAnimation:animation forKey:nil];
}
旋转效果
-(void)rotateForView:(UIView*)view
{
CALayer *viewLayer = view.layer;
CABasicAnimation*animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
animation.fromValue=@0;
animation.toValue=@(M_PI*2.0);
animation.cumulative = YES;
[animation setDuration:1.0];
[animation setRepeatCount:1];
[viewLayer addAnimation:animation forKey:nil];
}
大小变化效果
-(void)scaleForView:(UIView*)view
{
CALayer *viewLayer = view.layer;
CABasicAnimation*animation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
animation.fromValue=@1;
animation.toValue=@2;
// [animation setAutoreverses:YES];
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[animation setDuration:1.0];
[animation setRepeatCount:1];
[viewLayer addAnimation:animation forKey:nil];
}
还有许多动画效果,这里就不一一例举了,CABasicAnimation的key如下,有兴趣的话,可以用不同的key试试
animationWithKeyPath的值:
transform.scale = 比例轉換
transform.scale.x = 闊的比例轉換
transform.scale.y = 高的比例轉換
transform.rotation.z = 平面圖的旋轉
opacity = 透明度
margin
zPosition
backgroundColor 背景颜色
cornerRadius 圆角
borderWidth
bounds
contents
contentsRect
cornerRadius
frame
hidden
mask
masksToBounds
opacity
position
shadowColor
shadowOffset
shadowOpacity
shadowRadius
网友评论