一鍵集成APP引導頁,包括圖片啟動圖以及利用視頻做引導頁(利用視頻做引導頁采取 布袋的世界 的建議),并都擁有不同狀態(tài)下的引導頁樣式,省掉冗余的代碼,集成性高,使用方便。
1. SHStartPageView內(nèi)部代碼
SHStartPageView.h
//
// SHStartPageView.h
// SHStartPageView
//
// Created by CoderSun on 2017/3/28.
// Copyright ? 2017年 Mobby. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SHStartPageView : UIView
/**
根據(jù)圖片數(shù)組創(chuàng)建SHStartPageView
@param imageArray 引導頁圖片數(shù)組
@param isHidden 點擊進入按鈕是否隱藏
@return SHStartPageView
*/
- (instancetype)initWithImageArray:(NSArray *)imageArray
enterButtonIsHidden:(BOOL)isHidden;
/**
根據(jù)視頻名稱以及視頻類型創(chuàng)建SHStartPageView
@param videoName 視頻名稱
@param videoType 視頻格式
@param isHidden 點擊進入按鈕是否隱藏
@return SHStartPageView
*/
- (instancetype)initWithVideoName:(NSString *)videoName
videoType:(NSString *)videoType
enterButtonIsHidden:(BOOL)isHidden;
/**
根據(jù)視頻url創(chuàng)建SHStartPageView
@param videoURL 視頻url
@param isHidden 點擊進入按鈕是否隱藏
@return SHStartPageView
*/
- (instancetype)initWithVideoURL:(NSURL *)videoURL
enterButtonIsHidden:(BOOL)isHidden;
@end
SHStartPageView.m
//
// SHStartPageView.m
// SHStartPageView
//
// Created by CoderSun on 2017/3/28.
// Copyright ? 2017年 Mobby. All rights reserved.
//
#define MainScreen_width [UIScreen mainScreen].bounds.size.width
#define MainScreen_height [UIScreen mainScreen].bounds.size.height
#import "SHStartPageView.h"
#import <AVFoundation/AVFoundation.h>
@interface SHStartPageView ()<UIScrollViewDelegate>
// image - SHStartPageView
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) UIScrollView *bigScrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
// video - SHStartPageView
@property (nonatomic, strong) UIView *videoView;
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerItem *playerItem;
@property (nonatomic, strong) NSURL *videoURL;
@end
@implementation SHStartPageView
- (instancetype)initWithImageArray:(NSArray *)imageArray
enterButtonIsHidden:(BOOL)isHidden{
if (self = [super initWithFrame:CGRectMake(0, 0, MainScreen_width, MainScreen_height)]) {
_imageArray = imageArray;
[self drawImageSHStartPageView:isHidden];
}
return self;
}
- (instancetype)initWithVideoName:(NSString *)videoName videoType:(NSString *)videoType enterButtonIsHidden:(BOOL)isHidden{
NSString *pathString = [[NSBundle mainBundle] pathForResource:videoName ofType:videoType];
NSURL *videoURL = [NSURL fileURLWithPath:pathString];
return [self initWithVideoURL:videoURL enterButtonIsHidden:isHidden];
}
- (instancetype)initWithVideoURL:(NSURL *)videoURL enterButtonIsHidden:(BOOL)isHidden{
if (self = [super initWithFrame:CGRectMake(0, 0, MainScreen_width, MainScreen_height)]) {
self.videoURL = videoURL;
[self drawVideoSHStartPageViewWithURL:self.videoURL isHidden:isHidden];
}
return self;
}
// 繪制視頻啟動圖
- (void)drawVideoSHStartPageViewWithURL:(NSURL *)url isHidden:(BOOL)isHidden{
self.videoView = [[UIView alloc] initWithFrame:self.frame];
[self addSubview:self.videoView];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
self.playerItem = playerItem;
self.player = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
/*
videoGravity:
AVLayerVideoGravityResizeAspect:按原視頻比例顯示,是豎屏的就顯示出豎屏的辣辫,兩邊留黑
AVLayerVideoGravityResizeAspectFill:以原比例拉伸視頻,直到兩邊屏幕都占滿,但視頻內(nèi)容有部分就被切割了
AVLayerVideoGravityResize:拉伸視頻內(nèi)容達到邊框占滿队他,但不按原比例拉伸,這里明顯可以看出寬度被拉伸了
*/
layer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
[self.videoView.layer addSublayer:layer];
[self.player play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
if (isHidden == NO) {
UIButton *enterButton = [UIButton buttonWithType:UIButtonTypeCustom];
enterButton.frame = CGRectMake(self.videoView.frame.size.width / 2 - 50, MainScreen_height - 150, 100, 50);
[enterButton setTitle:@"點擊進入" forState:UIControlStateNormal];
enterButton.layer.borderColor = [UIColor whiteColor].CGColor;
enterButton.layer.borderWidth = 1;
[enterButton addTarget:self action:@selector(skipAction:) forControlEvents:UIControlEventTouchUpInside];
[self.videoView addSubview:enterButton];
[self.videoView bringSubviewToFront:enterButton];
}
}
// 繪制圖片啟動圖
- (void)drawImageSHStartPageView:(BOOL)isHidden{
// scrollView
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, MainScreen_width, MainScreen_height)];
scrollView.contentSize = CGSizeMake((_imageArray.count + 1) * MainScreen_width, MainScreen_height);
scrollView.pagingEnabled = YES;
scrollView.bounces = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.delegate = self;
[self addSubview:scrollView];
_bigScrollView = scrollView;
// 添加引導圖
for (int i = 0; i < _imageArray.count; i ++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * MainScreen_width, 0, MainScreen_width, MainScreen_height)];
imageView.image = [UIImage imageNamed:_imageArray[i]];
imageView.userInteractionEnabled = YES;
[scrollView addSubview:imageView];
if (i == _imageArray.count - 1 && isHidden == NO) {
// 點擊進入按鈕
UIButton *enterButton = [UIButton buttonWithType:UIButtonTypeCustom];
enterButton.frame = CGRectMake(imageView.frame.size.width/2 - 50, MainScreen_height - 150, 100, 50);
[enterButton setTitle:@"點擊進入" forState:UIControlStateNormal];
enterButton.layer.borderColor = [UIColor whiteColor].CGColor;
enterButton.layer.borderWidth = 1;
[enterButton addTarget:self action:@selector(skipAction:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:enterButton];
}else{
// 跳過按鈕
UIButton *skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
skipButton.frame = CGRectMake(imageView.frame.size.width - 70, 20, 60, 30);
[skipButton setTitle:@"跳過" forState:UIControlStateNormal];
skipButton.titleLabel.font = [UIFont systemFontOfSize:20];
[skipButton setTintColor:[UIColor lightGrayColor]];
[skipButton setBackgroundColor:[UIColor grayColor]];
[skipButton.layer setCornerRadius:5.0];
[skipButton addTarget:self action:@selector(skipAction:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:skipButton];
}
}
// 頁面控制器
UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(MainScreen_width / 2, MainScreen_height - 60, 0, 40)];
pageControl.numberOfPages = _imageArray.count;
pageControl.backgroundColor = [UIColor clearColor];
[self addSubview:pageControl];
_pageControl = pageControl;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (scrollView == _bigScrollView) {
CGPoint offset = scrollView.contentOffset;
_pageControl.currentPage = offset.x / MainScreen_width;
}
if (scrollView.contentOffset.x == (_imageArray.count) * MainScreen_width) {
[self removeFromSuperview];
}
}
- (void)skipAction:(UIButton *)sender{
if (self.player) {
[self removePlayer];
}
[self removeFromSuperview];
}
- (void)removePlayer{
[self.player pause];
self.player = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - Notification
- (void)moviePlayDidEnd:(NSNotification *)notification{
NSLog(@"播放完畢");
if (self.player) {
[self removePlayer];
}
[self removeFromSuperview];
}
@end
2. SHStartPageView使用
簡單使用:
// 根據(jù)圖片數(shù)組創(chuàng)建啟動圖
NSArray *imageArray = @[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg"];
SHStartPageView *shStartPageView = [[SHStartPageView alloc] initWithImageArray:imageArray enterButtonIsHidden:NO];
[self.window.rootViewController.view addSubview: shStartPageView];
// 根據(jù)視頻名稱和類型創(chuàng)建啟動圖
SHStartPageView *shStartPageView = [[SHStartPageView alloc] initWithVideoName:@"1" videoType:@"mp4" enterButtonIsHidden:NO];
[self.window.rootViewController.view addSubview:shStartPageView];
常用用法:
// 判斷第一次進入
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
NSLog(@"第一次進入");
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
// 根據(jù)圖片數(shù)組創(chuàng)建啟動圖
NSArray *imageArray = @[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg"];
SHStartPageView *shStartPageView = [[SHStartPageView alloc] initWithFrame:self.window.frame ImageArray:imageArray enterButtonIsHidden:NO];
// 根據(jù)視頻名稱和類型創(chuàng)建啟動圖
SHStartPageView *shStartPageView = [[SHStartPageView alloc] initWithVideoName:@"1" videoType:@"mp4" enterButtonIsHidden:NO];
[self.window.rootViewController.view addSubview:shStartPageView];
}