因工作需要,高仿【喵播】的Demo核心功能基本完成。直播端音视频采集、推流、内置音乐播放器系统,播放端的视频拉流播放、直播聊天室即时通信等功能已经完成。不过今天不是要讲这些看似高大上的东西,今天还是继续说说直播APP中的一些细枝末节,哈哈,不要着急,等忙过这段时间我会系统的写一篇关于
iOS直播APP
的文章,不想错过的小伙伴,敬请关注!!!。
今天要说的是直播APP的点赞动画,看下图,这是我仿【喵播】做的界面,点击屏幕会有心型动画飘出;这些动画图片都是从喵播APP抓下来的,如果你还不知道怎么抓一个APP的图片素材,文章后面有介绍,绝对够惊喜。
下面进入正题:
点赞动画的实现逻辑:
1、点击屏幕触发点击事件,生成一个imageView并给其添加一张随机的图片
2、给imageView设置有一个初始frame值
3、开始动画,设置动画执行
4、给这个imageView设置一个终点frame值
5、结束动画
6、销毁imageView
这是动画的大致实现逻辑,有很多细节和参数配置问题,代码注释的很清楚,直接看代码就OK了:
- 给view添加点击事件:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 点赞
[self praiseAnimation];
}
- 动画核心代码:
#pragma mark - 点赞动画
- (void)praiseAnimation {
UIImageView *imageView = [[UIImageView alloc] init];
CGRect frame = self.view.frame;
// 初始frame,即设置了动画的起点
imageView.frame = CGRectMake(frame.size.width - 40, frame.size.height - 65, 30, 30);
// 初始化imageView透明度为0
imageView.alpha = 0;
imageView.backgroundColor = [UIColor clearColor];
imageView.clipsToBounds = YES;
// 用0.2秒的时间将imageView的透明度变成1.0,同时将其放大1.3倍,再缩放至1.1倍,这里参数根据需求设置
[UIView animateWithDuration:0.2 animations:^{
imageView.alpha = 1.0;
imageView.frame = CGRectMake(frame.size.width - 40, frame.size.height - 90, 30, 30);
CGAffineTransform transfrom = CGAffineTransformMakeScale(1.3, 1.3);
imageView.transform = CGAffineTransformScale(transfrom, 1, 1);
}];
[self.view addSubview:imageView];
// 随机产生一个动画结束点的X值
CGFloat finishX = frame.size.width - round(random() % 200);
// 动画结束点的Y值
CGFloat finishY = 200;
// imageView在运动过程中的缩放比例
CGFloat scale = round(random() % 2) + 0.7;
// 生成一个作为速度参数的随机数
CGFloat speed = 1 / round(random() % 900) + 0.6;
// 动画执行时间
NSTimeInterval duration = 4 * speed;
// 如果得到的时间是无穷大,就重新附一个值(这里要特别注意,请看下面的特别提醒)
if (duration == INFINITY) duration = 2.412346;
// 随机生成一个0~7的数,以便下面拼接图片名
int imageName = round(random() % 8);
// 开始动画
[UIView beginAnimations:nil context:(__bridge void *_Nullable)(imageView)];
// 设置动画时间
[UIView setAnimationDuration:duration];
// 拼接图片名字
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"good%d_30x30_.png",imageName]];
// 设置imageView的结束frame
imageView.frame = CGRectMake( finishX, finishY, 30 * scale, 30 * scale);
// 设置渐渐消失的效果,这里的时间最好和动画时间一致
[UIView animateWithDuration:duration animations:^{
imageView.alpha = 0;
}];
// 结束动画,调用onAnimationComplete:finished:context:函数
[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
// 设置动画代理
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
- 动画完成后执行:
/// 动画完后销毁iamgeView
- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
UIImageView *imageView = (__bridge UIImageView *)(context);
[imageView removeFromSuperview];
imageView = nil;
}
至此就完成了简单的点赞功能,很简单吧!
特别提醒:
// 生成一个作为速度参数的随机数
CGFloat speed = 1 / round(random() % 900) + 0.6;
// 动画执行时间
NSTimeInterval duration = 4 * speed;
NSLog(@"时间--%f",duration);
执行上面这段代码,打印结果,发现偶尔会打印出inf
,inf
是无穷大的意思,出现这个值,也就意味着你屏幕上会有一个图片停在那不动,这就尴尬啦!
所以要加上这句判断:
// 如果得到的时间是无穷大,就重新附一个值(这里要特别注意,请看下面的特别提醒)
if (duration == INFINITY) duration = 2.412346;
```
下面绝对的大**彩蛋**:
**抓取APP素材的方法:**
1、下载安装[iOS images Extractor](https://pan.baidu.com/s/1dE1zgEd)到你的Mac上
2、在iTunes的AppStore上下载你需要抓取素材的APP
3、进入iTunes的`我的应用`,找到你刚下载的APP,右击选择`在Finder中显示`
![B498B849-4B22-451F-89F4-9253186BE581.png](http://upload-images.jianshu.io/upload_images/1707533-6282076d2484ecb7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* Finder中显示的APP ipa文件
![87A0C97F-E041-4A4B-9A34-DC69EE91809A.png](http://upload-images.jianshu.io/upload_images/1707533-4812307815b0bc0d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
4、打开[iOS images Extractor](https://pan.baidu.com/s/1dE1zgEd),直接将APP ipa拖进去,点击start等几秒,OK完成了。
![2E845738-9BA5-4B17-8B6D-15716B02D111.png](http://upload-images.jianshu.io/upload_images/1707533-13d44b0e7f9c584b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
5、在 `下载`文件中找到APP素材(文件保存路径你可以自己设置,不再赘述)
![CC37D6CF-E395-48F9-A3A9-2D0E707317D9.png](http://upload-images.jianshu.io/upload_images/1707533-072b482cec78c8c5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
是不是够惊喜,用[iOS images Extractor](https://pan.baidu.com/s/1dE1zgEd)抓取APP内的图片就是这么简单,在也不同担心以后写Demo界面丑到不能看了。哈哈,今天就说这么多,喜欢的点赞加关注吧!!!