直播送礼物动画

之前公司一直在搞直播,需要弄送礼物动画,当时也不知道怎么做,本人也是小白一个,在网上找demo学习,现在把我学到的总结一下.
效果图:


Snip20161227_15.png

送鲜花参考http://www.jianshu.com/p/ca1e7cdafa48
送豪车是用关键帧动画,关键帧动画可以参考http://www.jianshu.com/p/a071bba99a1b
下面是代码:

- (void)animation {

    [UIView animateKeyframesWithDuration:4.0f delay:0.2 options:0 animations:^{
        
        __block CGPoint center = self.center;
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1.0/4 animations:^{
            self.transform = CGAffineTransformMakeScale(2, 2);
        }];
        
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1.5/4 animations:^{
            self.center = (CGPoint){center.x + [UIScreen mainScreen].bounds.size.width / 2 - 50, center.y + 200};
        }];
        
        [UIView addKeyframeWithRelativeStartTime:1.5/4 relativeDuration:2.5/4 animations:^{
            self.center = (CGPoint){center.x + center.x + [UIScreen mainScreen].bounds.size.width / 2 - 40, center.y + 210};
        }];
        
        [UIView addKeyframeWithRelativeStartTime:2.5/4 relativeDuration:3.5/4 animations:^{
            self.center = (CGPoint){center.x + [UIScreen mainScreen].bounds.size.width -50, center.y + 260};
        }];
        
        [UIView addKeyframeWithRelativeStartTime:3.5/4 relativeDuration:4.0/4 animations:^{
            self.center = (CGPoint){center.x + [UIScreen mainScreen].bounds.size.width + 50, center.y + 270};
        }];
        
    } completion:^(BOOL finished) {
        NSLog(@"删除动画");
        [self removeFromSuperview];
    }];
}

直播点赞,粒子动画,代码:

-(CAEmitterLayer *)emitterLayer {
    if (_emitterLayer == nil) {
        _emitterLayer = [CAEmitterLayer layer];
        //发射器在xy轴上的位置
        _emitterLayer.emitterPosition = CGPointMake(self.view.frame.size.width - 50, self.view.frame.size.height - 50);
        //发射器的尺寸大小
        _emitterLayer.emitterSize = CGSizeMake(20, 20);
        //渲染模式
        _emitterLayer.renderMode = kCAEmitterLayerUnordered;
        _emitterLayer.emitterShape = kCAEmitterLayerPoint;
        
        //创建保存粒子的数组
        NSMutableArray *array = [NSMutableArray array];
        //创建粒子
        for (int i = 0; i < 10; ++i) {
            //发射单元
            CAEmitterCell *stepCell = [CAEmitterCell emitterCell];
            //粒子创建速率 默认1/s
            stepCell.birthRate = 1;
            //粒子存活时间
            stepCell.lifetime = arc4random_uniform(4) + 1;
            //粒子的生存时间容差
            stepCell.lifetimeRange = 1.5;
            //粒子的内容
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"good%d_30x30",i]];
            stepCell.contents = (id)image.CGImage;
            //粒子的名字
            stepCell.name = [NSString stringWithFormat:@"%d",i];
            //粒子的运动速率
            stepCell.velocity = arc4random_uniform(100) + 100;
            //粒子速度的容差
            stepCell.velocityRange = 80;
            //粒子在xy平面的发射角度
            stepCell.emissionLongitude = M_PI + M_PI_2;
            //粒子发射角度容差
            stepCell.emissionRange = M_PI_2/6;
            //缩放比例
            stepCell.scale = 0.3;
            //添加到数组中
            [array addObject:stepCell];
        }
        //将粒子数组放入发射器中
        _emitterLayer.emitterCells = array;
        //将发射器添加到父类视图
        [self.view.layer addSublayer:_emitterLayer];
    }
    return _emitterLayer;
}

在下小白一个,以此在记录个人的成长,希望跟大家一起共同学习.

demo:https://github.com/xiongHenry/XT_SendGiftDemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容