ios开发中用视频作为背景,视频上可以覆盖文字、按钮等
2014-07-25 16:14
267 查看
直接上代码:
//
// HelpVidoViewController.h
// Goccia
//
// Created by JackMeng on 14-7-16.
// Copyright (c) 2014年 g-wearables.com. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface HelpVidoViewController : UIViewController
@end
-----------------------------我是分割线--------------------------------------------------------------------------------
//
// HelpVidoViewController.m
// Goccia
//
// Created by JackMeng on 14-7-16.
// Copyright (c) 2014年 g-wearables.com. All rights reserved.
//
#import "HelpVidoViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface HelpVidoViewController ()
{
UIApplication *app;
}
@property (nonatomic, strong) AVPlayer *avplayer;
@property (strong, nonatomic) UIView *movieView;
@property (strong, nonatomic) UIView *gradientView;
@property (strong, nonatomic) UIView *contentView;
@end
@implementation HelpVidoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.avplayer seekToTime:kCMTimeZero];
// [self.avplayer setVolume:0.50f];
[self.avplayer play];
[self.avplayer setActionAtItemEnd:AVPlayerActionAtItemEndNone];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(helpPlayerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.avplayer currentItem]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.movieView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.movieView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.movieView];
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"xxxx" ofType:@"mov"];//xxxx代表你要播放的视频
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
AVAsset *avAsset = [AVAsset assetWithURL:movieURL];
AVPlayerItem *avPlayerItem =[[AVPlayerItem alloc]initWithAsset:avAsset];
self.avplayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];
AVPlayerLayer *avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:self.avplayer];
[avPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
[avPlayerLayer setFrame:self.view.frame];
[self.movieView.layer addSublayer:avPlayerLayer];
self.gradientView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.gradientView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.gradientView];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.gradientView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor clearColor] CGColor], (id)[[UIColor clearColor] CGColor],nil];
[self.gradientView.layer insertSublayer:gradient atIndex:0];
self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.contentView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.contentView];
UILabel * helpLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 300, 50)];
helpLabel.text = NSLocalizedString(@"Tutorial", @"使用教程");
helpLabel.textAlignment = NSTextAlignmentCenter;
helpLabel.font = [UIFont systemFontOfSize:40.0];
helpLabel.textColor = [UIColor whiteColor];
[self.contentView addSubview:helpLabel];
UIButton * dismissBut = [UIButton buttonWithType:UIButtonTypeCustom];
if (IS_IPHONE5)
{
dismissBut.frame = CGRectMake(240, 528, 80, 40);
}
else
{
dismissBut.frame = CGRectMake(240, 440, 80, 40);
}
[dismissBut setTitle:NSLocalizedString(@"Skip", @"跳过") forState:UIControlStateNormal];
dismissBut.titleLabel.font = [UIFont systemFontOfSize:14.0];
[dismissBut addTarget:self action:@selector(dismissButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:dismissBut];
app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(helpApplicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:app];
UITapGestureRecognizer * playTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playTap_help)];
[self.contentView addGestureRecognizer:playTap];
}
- (void)helpApplicationDidBecomeActive:(NSNotification *)notification
{
[self.avplayer play];
}
- (void) playTap_help
{
if (self.avplayer.status == 1)
{
[self.avplayer play];
}
}
- (void) dismissButtonClick
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:app];
[self dismissViewControllerAnimated:YES completion:^{}];
}
- (void)helpPlayerItemDidReachEnd:(NSNotification *)notification
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.avplayer pause];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// HelpVidoViewController.h
// Goccia
//
// Created by JackMeng on 14-7-16.
// Copyright (c) 2014年 g-wearables.com. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface HelpVidoViewController : UIViewController
@end
-----------------------------我是分割线--------------------------------------------------------------------------------
//
// HelpVidoViewController.m
// Goccia
//
// Created by JackMeng on 14-7-16.
// Copyright (c) 2014年 g-wearables.com. All rights reserved.
//
#import "HelpVidoViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface HelpVidoViewController ()
{
UIApplication *app;
}
@property (nonatomic, strong) AVPlayer *avplayer;
@property (strong, nonatomic) UIView *movieView;
@property (strong, nonatomic) UIView *gradientView;
@property (strong, nonatomic) UIView *contentView;
@end
@implementation HelpVidoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.avplayer seekToTime:kCMTimeZero];
// [self.avplayer setVolume:0.50f];
[self.avplayer play];
[self.avplayer setActionAtItemEnd:AVPlayerActionAtItemEndNone];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(helpPlayerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.avplayer currentItem]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.movieView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.movieView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.movieView];
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"xxxx" ofType:@"mov"];//xxxx代表你要播放的视频
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
AVAsset *avAsset = [AVAsset assetWithURL:movieURL];
AVPlayerItem *avPlayerItem =[[AVPlayerItem alloc]initWithAsset:avAsset];
self.avplayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];
AVPlayerLayer *avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:self.avplayer];
[avPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
[avPlayerLayer setFrame:self.view.frame];
[self.movieView.layer addSublayer:avPlayerLayer];
self.gradientView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.gradientView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.gradientView];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.gradientView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor clearColor] CGColor], (id)[[UIColor clearColor] CGColor],nil];
[self.gradientView.layer insertSublayer:gradient atIndex:0];
self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.contentView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.contentView];
UILabel * helpLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 300, 50)];
helpLabel.text = NSLocalizedString(@"Tutorial", @"使用教程");
helpLabel.textAlignment = NSTextAlignmentCenter;
helpLabel.font = [UIFont systemFontOfSize:40.0];
helpLabel.textColor = [UIColor whiteColor];
[self.contentView addSubview:helpLabel];
UIButton * dismissBut = [UIButton buttonWithType:UIButtonTypeCustom];
if (IS_IPHONE5)
{
dismissBut.frame = CGRectMake(240, 528, 80, 40);
}
else
{
dismissBut.frame = CGRectMake(240, 440, 80, 40);
}
[dismissBut setTitle:NSLocalizedString(@"Skip", @"跳过") forState:UIControlStateNormal];
dismissBut.titleLabel.font = [UIFont systemFontOfSize:14.0];
[dismissBut addTarget:self action:@selector(dismissButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:dismissBut];
app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(helpApplicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:app];
UITapGestureRecognizer * playTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playTap_help)];
[self.contentView addGestureRecognizer:playTap];
}
- (void)helpApplicationDidBecomeActive:(NSNotification *)notification
{
[self.avplayer play];
}
- (void) playTap_help
{
if (self.avplayer.status == 1)
{
[self.avplayer play];
}
}
- (void) dismissButtonClick
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:app];
[self dismissViewControllerAnimated:YES completion:^{}];
}
- (void)helpPlayerItemDidReachEnd:(NSNotification *)notification
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.avplayer pause];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- IOS开发中如何自定义导航栏的按钮,文字和背景图片
- IOS开发之UIButton同时设置图片文字以及触发按钮时文字图片的切换
- iOS开发navigationBar导航条和navigationItem设置:基本搞定导航条上的文字和按钮以及各种跳转
- 【Cocos2dx】用当前游戏状态作为暂停背景、纯文字纯图片按钮与不间断的重复动作序列
- iOS开发设置UISearchBar的输入背景框、提示文字等
- IOS开发OC 如何将导航栏左侧系统自动生成的返回按钮的文字去掉,只保留小于号"<"图案
- iOS开发备忘录:自定义UINavigationBar背景图片和Back按钮
- ios设置按钮点击时的文字和图片(而非非背景)
- ios之自定义导航栏背景,标题和返回按钮文字颜色
- IOS设置导航栏全局的背景图片、导航栏title文字颜色大小、导航栏左右按钮的颜色。
- 【iOS开发-22】navigationBar导航条和navigationItem设置:基本搞定导航条上的文字和按钮以及各种跳转
- 【iOS开发-22】navigationBar导航条和navigationItem设置:基本搞定导航条上的文字和按钮以及各种跳转
- IOS开发之UIButton同时设置图片文字以及出发按钮时文字图片的切换
- iOS开发备忘录:自定义UINavigationBar背景图片和Back按钮
- 抽奖滚动文字如何透明地覆盖在动态视频背景上?-活动现场双屏管理系统操作提示
- 【iOS开发-39】为什么按钮点击时背景图片颜色灰蒙蒙的?与按钮的Type属性有关
- 自定义iOS导航栏背景,标题和返回按钮文字颜色
- iOS开发--根据文字动态调整按钮(UIButton)宽度
- iOS开发之Swift标签栏按钮UITabBarItem样式修改(图标文字尺寸,颜色等)
- iOS开发swift中tableView的条目点击的背景和文字颜色