如何搞出像GameCenter中泡泡一样的动画

代码中加了关键注释,不多说了。

效果的话,想象水中气泡上升过程,只不过没有上升,只是摇来摇去。

- (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"];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值