您的位置:首页 > 移动开发 > IOS开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐