iOS中三种方式实现登录界面播放视频或gif效果
现在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效果的更多相关文章
- Django中三种方式写form表单
除了在html中自己手写form表单外,django还可以通过 继承django.forms.Form 或django.forms.ModelForm两个类来自动生成form表单,下面依次利用三种方式 ...
- java中三种方式获得类的字节码文件对象
package get_class_method; public class ReflectDemo { /** * @param args */ public static void main(St ...
- iOS——浅谈iOS中三种生成随机数方法
ios 有如下三种随机数方法:
- iOS中 三种随机数方法详解
ios 有如下三种随机数方法: //第一种 srand((unsigned)time(0)); //不加这句每次产生的随机数不变 int i = rand() % 5; //第二种 srandom(t ...
- iOS-浅谈iOS中三种生成随机数方法
ios 有如下三种随机数方法:
- ios中三种多线程的技术对比
1.NSThread 使用较少 在NSThread调用的方法中,同样要使用autoreleasepool进行内存管理,否则容易出现内存泄露. 使用流程:创建线程-->启动线程 2.NSOpera ...
- java实现HTTP请求的三种方式
目前JAVA实现HTTP请求的方法用的最多的有两种:一种是通过HTTPClient这种第三方的开源框架去实现.HTTPClient对HTTP的封装性比较不错,通过它基本上能够满足我们大部分的需求,Ht ...
- 【转载】java实现HTTP请求的三种方式
目前JAVA实现HTTP请求的方法用的最多的有两种:一种是通过HTTPClient这种第三方的开源框架去实现.HTTPClient对HTTP的封装性比较不错,通过它基本上能够满足我们大部分的需求,Ht ...
- ios学习网络------4 UIWebView以三种方式中的本地数据
UIWebView这是IOS内置的浏览器.能够浏览网页,打开文档 html/htm pdf docx txt等待格文档类型. safari浏览器是通过UIWebView制作. server将 ...
随机推荐
- java中split任意数量的空白字符
java程序中经常通过split截取字符串来取得其中的关键字.但是由于其中不同操作系统或者不同作者的习惯,经常会出现不同数量的空格,例如" "(3个空格):或者出现制表符.Ta ...
- SQL表操作习题1
建表
- jdbc多种实现方式
1,驱动加载 //注册驱动 //DriverManager.registerDriver(new Driver());此方法被淘汰 Class.forName("com.mysql.jdbc ...
- 集合框架(04)HashMap扩展知识
Map扩展知识 map集合被使用是具备映射关系 “bigclass”: “001”, ”zhangsan” “002”, ”lisi” “smallclass” : ”001”, “wangwu” : ...
- iOS9 Storyboard unwind segue反回传递事件时机详细步骤
当返回上一个界面且需要上一个界面做某事时,用unwind segue实现起来比delegate简单许多,甚至有时不适合用delegate来实现,那么我们就用unwind segue吧,而且像1-> ...
- MySQL 三节点企业版
https://promotion.aliyun.com/ntms/act/rds/mysqlenterprise.html
- Android Handler 消息循环机制
前言 一问起Android应用程序的入口,很多人会说是Activity中的onCreate方法,也有人说是ActivityThread中的静态main方法.因为Java虚拟机在运行的时候会自动加载指定 ...
- Glide使用详解(一)
一. 下载 在build.gradle中添加依赖: compile 'com.github.bumptech.glide:glide:3.7.0' 需要support-v4库的支持,如果你的项目没有s ...
- 去除自定义Toolbar中左边距
问题 自定义Toolbar之后,发现左侧不能完全填充,总是留一点空白,如下图: 原因 查看Wiget.AppCompat.Toolbar的parent(Toolbar默认的style),如下: < ...
- 自动生成logo的网址
1.https://www.logaster.com/logo/