代码中加了关键注释,不多说了。
效果的话,想象水中气泡上升过程,只不过没有上升,只是摇来摇去。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 40, 40)];
//圆角设置
view1.layer.masksToBounds = YES;
view1.layer.cornerRadius = 20;
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];
[self AddAniamtionLikeGameCenterBubble];
}
-(void)AddAniamtionLikeGameCenterBubble{
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced; //动画均匀进行
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.repeatCount = INFINITY;
//线性运动
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
pathAnimation.duration = 5.0;
//除了xy方向的扩大缩小,泡泡的位置按椭圆形轨迹运动
CGMutablePathRef curvedPath = CGPathCreateMutable();
// CGRect circleContainer = CGRectInset(view1.frame, view1.bounds.size.width / 2 -10, view1.bounds.size.width / 2 -5);
CGRect circleContainer = CGRectMake(200, 200, 10, 5);
//该方法 会按给定rect搞出一个内切圆出来,可以是椭圆
CGPathAddEllipseInRect(curvedPath, NULL, circleContainer);
pathAnimation.path = curvedPath;
//在上面通过creat创建出来的,需要释放
CGPathRelease(curvedPath);
[view1.layer addAnimation:pathAnimation forKey:@"CircleAnimation"];
CAKeyframeAnimation *scaleX = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];
scaleX.duration = 1;
scaleX.values = @[@1.0, @1.1, @1.0];
scaleX.keyTimes = @[@0.0, @0.5, @1.0];
//无线循环
scaleX.repeatCount = INFINITY;
scaleX.autoreverses = YES;
//运动时间函数 这个是先快后慢再快
scaleX.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[view1.layer addAnimation:scaleX forKey:@"scaleXAnimation"];
CAKeyframeAnimation *scaleY = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];
scaleY.duration = 1.5;
scaleY.values = @[@1.0, @1.1, @1.0];
scaleY.keyTimes = @[@0.0, @0.5, @1.0];
scaleY.repeatCount = INFINITY;
scaleY.autoreverses = YES;
scaleX.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[view1.layer addAnimation:scaleY forKey:@"scaleYAnimation"];
}