- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self initUserGuidePage]; //用户引导页
}
// 用户引导页
-(void)initUserGuidePage{
if (![[NSUserDefaults standardUserDefaults] boolForKey:BOOLFORKEY]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:BOOLFORKEY];
//静态引导页
[self setStaticGuidePage];
}
}
// 设置APP静态图片引导页
- (void)setStaticGuidePage {
NSArray *imageNameArray = @[@"one",@"two",@"three"];
DHGuidePageHUD *guidePage = [[DHGuidePageHUD alloc] dh_initWithFrame:[UIScreen mainScreen].bounds imageNameArray:imageNameArray buttonIsHidden:NO];
guidePage.slideInto = YES;
AppDelegate * del = (AppDelegate *)[UIApplication sharedApplication].delegate;
[del.window addSubview:guidePage];
}
在使用中只需要在跟视图的viewDidLoad里调用即可,调用方法如下:
//用户引导页
[(AppDelegate*)[UIApplication sharedApplication].delegate initUserGuidePage];
DHGuidePageHUD.h类
#import <UIKit/UIKit.h>
#define BOOLFORKEY @"dhGuidePage"
@interface DHGuidePageHUD : UIView
/**
* 是否支持滑动进入APP(默认为NO-不支持滑动进入APP | 只有在buttonIsHidden为YES-隐藏状态下可用; buttonIsHidden为NO-显示状态下直接点击按钮进入)
* 新增视频引导页同样不支持滑动进入APP
*/
@property (nonatomic, assign) BOOL slideInto;
/**
* DHGuidePageHUD(图片引导页 | 可自动识别动态图片和静态图片)
*
* @param frame 位置大小
* @param imageNameArray 引导页图片数组(NSString)
* @param isHidden 开始体验按钮是否隐藏(YES:隐藏-引导页完成自动进入APP首页; NO:不隐藏-引导页完成点击开始体验按钮进入APP主页)
*
* @return DHGuidePageHUD对象
*/
- (instancetype)dh_initWithFrame:(CGRect)frame imageNameArray:(NSArray<NSString *> *)imageNameArray buttonIsHidden:(BOOL)isHidden;
/**
* DHGuidePageHUD(视频引导页)
*
* @param frame 位置大小
* @param videoURL 引导页视频地址
*
* @return DHGuidePageHUD对象
*/
- (instancetype)dh_initWithFrame:(CGRect)frame videoURL:(NSURL *)videoURL;
@end
DHGuidePageHUD.m类
#import "DHGuidePageHUD.h"
#import "DHGifImageOperation.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVKit/AVKit.h>
#define DDHidden_TIME 3.0
#define DDScreenW [UIScreen mainScreen].bounds.size.width
#define DDScreenH [UIScreen mainScreen].bounds.size.height
@interface DHGuidePageHUD ()<UIScrollViewDelegate>
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) UIPageControl *imagePageControl;
@property (nonatomic, assign) NSInteger slideIntoNumber;
@property (nonatomic, strong) MPMoviePlayerController *playerController;
@end
@implementation DHGuidePageHUD
- (instancetype)dh_initWithFrame:(CGRect)frame imageNameArray:(NSArray<NSString *> *)imageNameArray buttonIsHidden:(BOOL)isHidden {
if ([super initWithFrame:frame]) {
self.slideInto = NO;
if (isHidden == YES) {
self.imageArray = imageNameArray;
}
// 设置引导视图的scrollview
UIScrollView *guidePageView = [[UIScrollView alloc]initWithFrame:frame];
[guidePageView setBackgroundColor:[UIColor lightGrayColor]];
[guidePageView setContentSize:CGSizeMake(DDScreenW*imageNameArray.count, DDScreenH)];
[guidePageView setBounces:NO];
[guidePageView setPagingEnabled:YES];
[guidePageView setShowsHorizontalScrollIndicator:NO];
[guidePageView setDelegate:self];
[self addSubview:guidePageView];
// 设置引导页上的跳过按钮
UIButton *skipButton = [[UIButton alloc]initWithFrame:CGRectMake(DDScreenW*0.8, DDScreenW*0.1, 50, 25)];
[skipButton setTitle:@"跳过" forState:UIControlStateNormal];
[skipButton.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
[skipButton setBackgroundColor:[UIColor grayColor]];
// [skipButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[skipButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
// [skipButton.layer setCornerRadius:5.0];
[skipButton.layer setCornerRadius:(skipButton.frame.size.height * 0.5)];
[skipButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:skipButton];
// 添加在引导视图上的多张引导图片
for (int i=0; i<imageNameArray.count; i++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(DDScreenW*i, 0, DDScreenW, DDScreenH)];
if ([[DHGifImageOperation dh_contentTypeForImageData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageNameArray[i] ofType:nil]]] isEqualToString:@"gif"]) {
NSData *localData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageNameArray[i] ofType:nil]];
imageView = (UIImageView *)[[DHGifImageOperation alloc] initWithFrame:imageView.frame gifImageData:localData];
[guidePageView addSubview:imageView];
} else {
imageView.image = [UIImage imageNamed:imageNameArray[i]];
[guidePageView addSubview:imageView];
}
// 设置在最后一张图片上显示进入体验按钮
if (i == imageNameArray.count-1 && isHidden == NO) {
[imageView setUserInteractionEnabled:YES];
UIButton *startButton = [[UIButton alloc]initWithFrame:CGRectMake(DDScreenW*0.3, DDScreenH*0.8, DDScreenW*0.4, DDScreenH*0.08)];
[startButton setTitle:@"开始体验" forState:UIControlStateNormal];
[startButton setTitleColor:[UIColor colorWithRed:164/255.0 green:201/255.0 blue:67/255.0 alpha:1.0] forState:UIControlStateNormal];
[startButton.titleLabel setFont:[UIFont systemFontOfSize:21]];
[startButton setBackgroundImage:[UIImage imageNamed:@"GuideImage.bundle/guideImage_button_backgound"] forState:UIControlStateNormal];
[startButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:startButton];
}
}
// 设置引导页上的页面控制器
self.imagePageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(DDScreenW*0.0, DDScreenH*0.9, DDScreenW*1.0, DDScreenH*0.1)];
self.imagePageControl.currentPage = 0;
self.imagePageControl.numberOfPages = imageNameArray.count;
self.imagePageControl.pageIndicatorTintColor = [UIColor grayColor];
self.imagePageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
[self addSubview:self.imagePageControl];
}
return self;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollview {
int page = scrollview.contentOffset.x / scrollview.frame.size.width;
[self.imagePageControl setCurrentPage:page];
if (self.imageArray && page == self.imageArray.count-1 && self.slideInto == NO) {
[self buttonClick:nil];
}
if (self.imageArray && page < self.imageArray.count-1 && self.slideInto == YES) {
self.slideIntoNumber = 1;
}
if (self.imageArray && page == self.imageArray.count-1 && self.slideInto == YES) {
UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:nil action:nil];
if (swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionRight){
self.slideIntoNumber++;
if (self.slideIntoNumber == 3) {
[self buttonClick:nil];
}
}
}
}
- (void)buttonClick:(UIButton *)button {
[UIView animateWithDuration:DDHidden_TIME animations:^{
self.alpha = 0;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(DDHidden_TIME * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self performSelector:@selector(removeGuidePageHUD) withObject:nil afterDelay:1];
});
}];
}
- (void)removeGuidePageHUD {
[self removeFromSuperview];
}
/**< APP视频新特性页面(新增测试模块内容) */
- (instancetype)dh_initWithFrame:(CGRect)frame videoURL:(NSURL *)videoURL {
if ([super initWithFrame:frame]) {
self.playerController = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[self.playerController.view setFrame:frame];
[self.playerController.view setAlpha:1.0];
[self.playerController setControlStyle:MPMovieControlStyleNone];
[self.playerController setRepeatMode:MPMovieRepeatModeOne];
[self.playerController setShouldAutoplay:YES];
[self.playerController prepareToPlay];
[self addSubview:self.playerController.view];
// 视频引导页进入按钮
UIButton *movieStartButton = [[UIButton alloc] initWithFrame:CGRectMake(20, DDScreenH-30-40, DDScreenW-40, 40*SizeScale)];
[movieStartButton.layer setBorderWidth:1.0*SizeScale];
[movieStartButton.layer setCornerRadius:20.0*SizeScale];
[movieStartButton.layer setBorderColor:[UIColor whiteColor].CGColor];
[movieStartButton setTitle:@"开始体验" forState:UIControlStateNormal];
[movieStartButton setAlpha:0.0];
[self.playerController.view addSubview:movieStartButton];
[movieStartButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[UIView animateWithDuration:DDHidden_TIME animations:^{
[movieStartButton setAlpha:1.0];
}];
}
return self;
}
@end
326

被折叠的 条评论
为什么被折叠?



