在AppDelegate中實(shí)現(xiàn)努酸。
在AppDelegate中所有控件創(chuàng)建完成之后服爷, 寫引導(dǎo)頁代碼。注意:引導(dǎo)頁在第一次進(jìn)入軟件的時候才會出現(xiàn),因此每次進(jìn)入運(yùn)行的時候應(yīng)該先從沙盒中讀取仍源,判斷沙盒中是否含有文件心褐, 沒有文件的時候創(chuàng)建一個文件,以便下次運(yùn)行的時候就不再出現(xiàn)引導(dǎo)頁笼踩。具體代碼如下逗爹。
/** 核心代碼 */
#pragma mark ** 判斷是否第一次進(jìn)入
- (void)createGuidancePage {
NSLog(@"%@", NSHomeDirectory());
// 第一次進(jìn)入, 從沙盒中讀文件.
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"引導(dǎo)頁"]) {
// 沙盒中沒有嚎于, 寫一個文件.
[[NSUserDefaults standardUserDefaults] setBool:1 forKey:@"引導(dǎo)頁"];
NSArray *arrayOfImageView = [NSArray array];
//添加引導(dǎo)頁圖片
arrayOfImageView = @[@"yindaoye1", @"yindaoye2", @"yindaoye3"];
self.scrollViewForGuidancePage = [[UIScrollView alloc] init];
//注意:添加在window上
[self.window addSubview:self.scrollViewForGuidancePage];
[_scrollViewForGuidancePage release];
self.scrollViewForGuidancePage.bounces = NO;
self.scrollViewForGuidancePage.pagingEnabled = YES;
self.scrollViewForGuidancePage.showsHorizontalScrollIndicator = NO;
self.scrollViewForGuidancePage.userInteractionEnabled = YES;
self.scrollViewForGuidancePage.frame = self.window.frame;
self.scrollViewForGuidancePage.contentSize = CGSizeMake(self.window.frame.size.width * arrayOfImageView.count, 0);
self.scrollViewForGuidancePage.delegate = self;
self.pageControlForGuidancePage = [[UIPageControl alloc] init];
[self.window addSubview:self.pageControlForGuidancePage];
[_pageControlForGuidancePage release];
self.pageControlForGuidancePage.frame = CGRectMake(0, 0, self.window.frame.size.width / 2, 40);
self.pageControlForGuidancePage.center = CGPointMake(self.scrollViewForGuidancePage.frame.size.width / 2, self.scrollViewForGuidancePage.frame.size.height - 40);
self.pageControlForGuidancePage.numberOfPages = arrayOfImageView.count;
//for循環(huán), 在scrollView上添加照片
for (int i = 0; i < arrayOfImageView.count; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:arrayOfImageView[i]]];
imageView.frame = CGRectMake(self.scrollViewForGuidancePage.frame.size.width * i, 0, self.scrollViewForGuidancePage.frame.size.width, self.scrollViewForGuidancePage.frame.size.height);
[self.scrollViewForGuidancePage addSubview:imageView];
imageView.userInteractionEnabled = YES;
if (i == arrayOfImageView.count - 1) {
//引導(dǎo)頁最后一頁"開始體驗(yàn)"按鈕, 進(jìn)入APP
self.buttonForStart = [UIButton buttonWithType:UIButtonTypeCustom];
[imageView addSubview:self.buttonForStart];
[self.buttonForStart setTitle:@"開始體驗(yàn)" forState:UIControlStateNormal];
self.buttonForStart.layer.borderWidth = 1;
self.buttonForStart.layer.borderColor = [[UIColor whiteColor] CGColor];
self.buttonForStart.layer.cornerRadius = 7;
self.buttonForStart.layer.masksToBounds = YES;
self.buttonForStart.frame = CGRectMake(0, 0, self.window.frame.size.width / 2, 40);
self.buttonForStart.center = CGPointMake(self.window.frame.size.width / 2, self.window.frame.size.height - 100);
[self.buttonForStart addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
}
}
}
}
/** button 的點(diǎn)擊事件掘而,點(diǎn)擊之后進(jìn)入主頁. */
- (void)start {
//從父視圖上移除
[self.pageControlForGuidancePage removeFromSuperview];
[self.scrollViewForGuidancePage removeFromSuperview];
}
/** scrollView 的Delegate, 讓pageControl跟著scrollView的contentOffset的變化而變化. */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
self.pageControlForGuidancePage.currentPage = self.scrollViewForGuidancePage.contentOffset.x / self.window.frame.size.width;
}