现在app都做的越来越炫酷,各种动画效果,各种特效很好的提高了用户的体验。很多app在登录界面都使用了动画效果,比如Uber,Keep,QQ等等。这些动画效果基本都是使用gif或者MP4来实现的。

效果图:

这里提供3种方式实现登录界面动画,有需要的同学可以参考一下

1.video篇,使用avplayer加载video来实现效果

实现步骤基本上是初始化播放器,然后加载video,有几个注意点:1.导入头文件#import <AVFoundation/AVFoundation.h> 2.app进入后台后,video播放就停止了,这里采用通知的方式在app从后台进入前台时通知控制开启播放video 代码如下:

- (void)setupForAVplayerView
{
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:playerLayer];
} /**
* 初始化播放器
*/
- (AVPlayer *)player
{
if (!_player) {
AVPlayerItem *playerItem = [self getPlayItem];
_player = [AVPlayer playerWithPlayerItem:playerItem];
//设置重复播放
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone; // set this
//视频播放完发通知
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(__playerItemDidPlayToEndTimeNotification:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:nil]; }
return _player;
} - (AVPlayerItem *)getPlayItem
{
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"BridgeLoop-640p" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:filePath];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
return playerItem;
} - (void)__playerItemDidPlayToEndTimeNotification:(NSNotification *)sender
{
[_player seekToTime:kCMTimeZero]; // 设置从头继续播放
} //视频进入后台到前台视频暂停的处理方法
appDelegate.m中
- (void)applicationDidBecomeActive:(UIApplication *)application { [[NSNotificationCenter defaultCenter]postNotificationName:@"appBecomeActive" object:nil];
}
viewController.m
- (void)viewWillAppear:(BOOL)animated
{
//视频播放
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playVideos) name:@"appBecomeActive" object:nil];
}

2.gif篇 利用webView加载gif的方法来实现gif的播放

需要注意的是html中加载的gif路径不要搞错了,要不然gif无法加载出来 代码如下:

html:

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
<title>loadgif</title>
<style type="text/css">
*{
margin:0px;
padding:0px;
}
.loadGif {
width: 100%;
height: 100%;
/*设置背景*/
background: url(loadgif.gif) no-repeat center center;
/*让背景全屏*/
background-size: cover;
/*让div全屏*/
position:absolute;
}
</style>
</head> <body>
<div class="loadGif"> </div>
</body> </html>

oc中

-(void)setupWebView{
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:webView];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"loadGif.html" ofType:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]];
[webView loadRequest:request];
webView.userInteractionEnabled = NO; }

3.gif篇 这里仍然使用gif,如果你觉得使用html过于麻烦,自己对html又不是很熟悉,这里提供另外一种加载gif的方法,使用YYImge加载gif

需要注意的是YYImge需要添加一个静态库 libz.tbd 依赖 代码如下:

-(void)setupYYImageView{
NSString *path = [[NSBundle mainBundle] pathForResource:@"loadgif.gif" ofType:nil];
YYImage *image = [YYImage imageWithContentsOfFile:path];
YYAnimatedImageView *imgView = [[YYAnimatedImageView alloc] initWithFrame:self.view.bounds];
imgView.contentMode = UIViewContentModeScaleAspectFill;
imgView.image = image;
[self.view addSubview:imgView];
}

demo下载地址 :https://github.com/qqcc1388/LoginWithVideoDemo

转载请标注来源https://www.cnblogs.com/qqcc1388/p/7156384.html

iOS中三种方式实现登录界面播放视频或gif效果的更多相关文章

  1. Django中三种方式写form表单

    除了在html中自己手写form表单外,django还可以通过 继承django.forms.Form 或django.forms.ModelForm两个类来自动生成form表单,下面依次利用三种方式 ...

  2. java中三种方式获得类的字节码文件对象

    package get_class_method; public class ReflectDemo { /** * @param args */ public static void main(St ...

  3. iOS——浅谈iOS中三种生成随机数方法

    ios 有如下三种随机数方法:

  4. iOS中 三种随机数方法详解

    ios 有如下三种随机数方法: //第一种 srand((unsigned)time(0)); //不加这句每次产生的随机数不变 int i = rand() % 5; //第二种 srandom(t ...

  5. iOS-浅谈iOS中三种生成随机数方法

    ios 有如下三种随机数方法:

  6. ios中三种多线程的技术对比

    1.NSThread 使用较少 在NSThread调用的方法中,同样要使用autoreleasepool进行内存管理,否则容易出现内存泄露. 使用流程:创建线程-->启动线程 2.NSOpera ...

  7. java实现HTTP请求的三种方式

    目前JAVA实现HTTP请求的方法用的最多的有两种:一种是通过HTTPClient这种第三方的开源框架去实现.HTTPClient对HTTP的封装性比较不错,通过它基本上能够满足我们大部分的需求,Ht ...

  8. 【转载】java实现HTTP请求的三种方式

    目前JAVA实现HTTP请求的方法用的最多的有两种:一种是通过HTTPClient这种第三方的开源框架去实现.HTTPClient对HTTP的封装性比较不错,通过它基本上能够满足我们大部分的需求,Ht ...

  9. ios学习网络------4 UIWebView以三种方式中的本地数据

    UIWebView这是IOS内置的浏览器.能够浏览网页,打开文档  html/htm  pdf   docx  txt等待格文档类型. safari浏览器是通过UIWebView制作. server将 ...

随机推荐

  1. HDU 4034 Graph Floyd最短路

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 给你一个最短路的表,让你还原整个图,并使得边最少 题解: 这样想..这个表示通过floy ...

  2. HashSet、LinkedHashSet和TreeSet

    关键技术: HashSet采用散列函数对元素进行排序,是专门为快速查询而设计的.存入HashSet的对象必须定义hashCode方法. TreeSet采用红黑树的数据结构进行排序元素,使用它可以从Se ...

  3. 用swift开发自己的MacOS锁屏软件(一)

    最近看到了NearLock这款软件,感觉还是很不错的,当我兴致勃勃的安装了体验之后,发现效果和自己所想的差太多了,所以,便想着自己写一个吧. 刚开始当然是查资料之类的,不查不知道,一查吓一跳,国内基本 ...

  4. VMware开启虚拟化实现CentOS创建KVM

    参考: http://blog.csdn.net/liulove_micky/article/details/48343013

  5. 为Chrome多账户添加单独的快捷方式

    Chrome的多账户功能非常好用,每个账户都有自己的独立的收藏夹.个人设置等.但是,当你要使用的账户不是默认账户时,必须经过一个切换的操作.本文将简单的介绍一个如何各账户添加快捷方式,从而实现直接登陆 ...

  6. 工作组模式下专用队列(Private Queue)如何引用远程队列路径

    查了N久资料,包括MSDN的官方文档,对于同一工作组下,不同机器之间如何利用Private Queue(专用队列)来发送/接收消息,关于Path的引用一说,无非都是MachineName\privat ...

  7. linux之vim命令

    :tabe fn     在一个新的标签页中编辑文件fngt     切换到下一个标签页gT     切换到上一个标签页:tabr     切换到第一个标签页:tabl     切换到最后一个标签页: ...

  8. 【spring data jpa】【mybatis】通过反射实现 更新/保存 实体的任意字段的操作

    代码如下: //代码示例:例如保存时,传入下面两个字段 String filed;String content; //User代表要更新的实体,user即本对象 //filed代表要更改的字段,例如u ...

  9. uVa 12563 Jin Ge Jin Qu

    分析可知,虽然t<109,但是总曲目时间大于t,实际上t不会超过180*n+678.此问题涉及到两个目标信息,首先要求曲目数量最多,在此基础上要求所唱的时间尽量长.可以定义 状态dp[i][j] ...

  10. 设计模式之模板模式(PHP实现)

    github地址:https://github.com/ZQCard/design_pattern * 在模板模式(Template Pattern)中,一个抽象类公开定义了执行它的方法的方式/模板. ...