-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathLaunchIntroductionView.m
More file actions
202 lines (192 loc) · 7.66 KB
/
LaunchIntroductionView.m
File metadata and controls
202 lines (192 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// LaunchIntroductionView.m
// ZYGLaunchIntroductionDemo
//
// Created by ZhangYunguang on 16/4/7.
// Copyright © 2016年 ZhangYunguang. All rights reserved.
//
#import "LaunchIntroductionView.h"
static NSString *const kAppVersion = @"appVersion";
@interface LaunchIntroductionView ()<UIScrollViewDelegate>
{
UIScrollView *launchScrollView;
UIPageControl *page;
}
@end
@implementation LaunchIntroductionView
NSArray *images;
BOOL isScrollOut;//在最后一页再次滑动是否隐藏引导页
CGRect enterBtnFrame;
NSString *enterBtnImage;
NSString *storyboard;
#pragma mark - 创建对象-->>不带button
+(instancetype)sharedWithImages:(NSArray *)imageNames{
images = imageNames;
isScrollOut = YES;
LaunchIntroductionView *launch = [[LaunchIntroductionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height)];
launch.backgroundColor = [UIColor whiteColor];
return launch;
}
#pragma mark - 创建对象-->>带button
+(instancetype)sharedWithImages:(NSArray *)imageNames buttonImage:(NSString *)buttonImageName buttonFrame:(CGRect)frame{
images = imageNames;
isScrollOut = NO;
enterBtnFrame = frame;
enterBtnImage = buttonImageName;
LaunchIntroductionView *launch = [[LaunchIntroductionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height)];
launch.backgroundColor = [UIColor whiteColor];
return launch;
}
#pragma mark - 用storyboard创建的项目时调用,不带button
+ (instancetype)sharedWithStoryboardName:(NSString *)storyboardName images:(NSArray *)imageNames {
images = imageNames;
storyboard = storyboardName;
isScrollOut = YES;
LaunchIntroductionView *launch = [[LaunchIntroductionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height)];
launch.backgroundColor = [UIColor whiteColor];
return launch;
}
#pragma mark - 用storyboard创建的项目时调用,带button
+ (instancetype)sharedWithStoryboard:(NSString *)storyboardName images:(NSArray *)imageNames buttonImage:(NSString *)buttonImageName buttonFrame:(CGRect)frame{
images = imageNames;
isScrollOut = NO;
enterBtnFrame = frame;
storyboard = storyboardName;
enterBtnImage = buttonImageName;
LaunchIntroductionView *launch = [[LaunchIntroductionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height)];
launch.backgroundColor = [UIColor whiteColor];
return launch;
}
#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addObserver:self forKeyPath:@"currentColor" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"nomalColor" options:NSKeyValueObservingOptionNew context:nil];
if ([self isFirstLauch]) {
UIStoryboard *story;
if (storyboard) {
story = [UIStoryboard storyboardWithName:storyboard bundle:nil];
}
UIWindow *window = [UIApplication sharedApplication].windows.lastObject;
if (story) {
UIViewController * vc = story.instantiateInitialViewController;
window.rootViewController = vc;
[vc.view addSubview:self];
}else {
[window addSubview:self];
}
[self addImages];
}else{
[self removeFromSuperview];
}
}
return self;
}
#pragma mark - 判断是不是首次登录或者版本更新
-(BOOL )isFirstLauch{
//获取当前版本号
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *currentAppVersion = infoDic[@"CFBundleShortVersionString"];
//获取上次启动应用保存的appVersion
NSString *version = [[NSUserDefaults standardUserDefaults] objectForKey:kAppVersion];
//版本升级或首次登录
if (version == nil || ![version isEqualToString:currentAppVersion]) {
[[NSUserDefaults standardUserDefaults] setObject:currentAppVersion forKey:kAppVersion];
[[NSUserDefaults standardUserDefaults] synchronize];
return YES;
}else{
return NO;
}
}
#pragma mark - 添加引导页图片
-(void)addImages{
[self createScrollView];
}
#pragma mark - 创建滚动视图
-(void)createScrollView{
launchScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height)];
launchScrollView.showsHorizontalScrollIndicator = NO;
launchScrollView.bounces = NO;
launchScrollView.pagingEnabled = YES;
launchScrollView.delegate = self;
launchScrollView.contentSize = CGSizeMake(kScreen_width * images.count, kScreen_height);
[self addSubview:launchScrollView];
for (int i = 0; i < images.count; i ++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * kScreen_width, 0, kScreen_width, kScreen_height)];
imageView.image = [UIImage imageNamed:images[i]];
[launchScrollView addSubview:imageView];
if (i == images.count - 1) {
//判断要不要添加button
if (!isScrollOut) {
UIButton *enterButton = [[UIButton alloc] initWithFrame:CGRectMake(enterBtnFrame.origin.x, enterBtnFrame.origin.y, enterBtnFrame.size.width, enterBtnFrame.size.height)];
[enterButton setImage:[UIImage imageNamed:enterBtnImage] forState:UIControlStateNormal];
[enterButton addTarget:self action:@selector(enterBtnClick) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:enterButton];
imageView.userInteractionEnabled = YES;
}
}
}
page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kScreen_height - 50, kScreen_width, 30)];
page.numberOfPages = images.count;
page.backgroundColor = [UIColor clearColor];
page.currentPage = 0;
page.defersCurrentPageDisplay = YES;
[self addSubview:page];
}
#pragma mark - 进入按钮
-(void)enterBtnClick{
[self hideGuidView];
}
#pragma mark - 隐藏引导页
-(void)hideGuidView{
[UIView animateWithDuration:0.5 animations:^{
self.alpha = 0;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self removeFromSuperview];
});
}];
}
#pragma mark - scrollView Delegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
int cuttentIndex = (int)(scrollView.contentOffset.x + kScreen_width/2)/kScreen_width;
if (cuttentIndex == images.count - 1) {
if ([self isScrolltoLeft:scrollView]) {
if (!isScrollOut) {
return ;
}
[self hideGuidView];
}
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView == launchScrollView) {
int cuttentIndex = (int)(scrollView.contentOffset.x + kScreen_width/2)/kScreen_width;
page.currentPage = cuttentIndex;
}
}
#pragma mark - 判断滚动方向
-(BOOL )isScrolltoLeft:(UIScrollView *) scrollView{
//返回YES为向左反动,NO为右滚动
if ([scrollView.panGestureRecognizer translationInView:scrollView.superview].x < 0) {
return YES;
}else{
return NO;
}
}
#pragma mark - KVO监测值的变化
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"currentColor"]) {
page.currentPageIndicatorTintColor = self.currentColor;
}
if ([keyPath isEqualToString:@"nomalColor"]) {
page.pageIndicatorTintColor = self.nomalColor;
}
}
- (void)dealloc
{
[self removeObserver:self forKeyPath:@"currentColor"];
[self removeObserver:self forKeyPath:@"nomalColor"];
}
@end