? ? ? ? 每個app都會有引導(dǎo)頁, ?由于引導(dǎo)頁是在第一次啟動這個app的時候才會出現(xiàn), 出于他的這個特點(diǎn), ?我是這樣做的.
? ? ? ? 首先, 先把引導(dǎo)頁所在的控制器設(shè)置為根視圖控制器, 當(dāng)引導(dǎo)頁展示完之后, 再將正常時app的控制器設(shè)置為根視圖控制器.
#import "BaseViewController.h"
@interface MyViewController : BaseViewController
@end
#import "MyViewController.h"
@interface MyViewController (){
// 創(chuàng)建頁碼控制器
UIPageControl *pageControl;
// 判斷是否是第一次進(jìn)入應(yīng)用
BOOL flag;
}
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
[self creatSub];
}
- (void)creatSub {
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
for (int i=0; i<4; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"引導(dǎo)%d.png",i+1]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
// 在最后一頁創(chuàng)建按鈕
if (i == 3) {
// 必須設(shè)置用戶交互 否則按鍵無法操作
imageView.userInteractionEnabled = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(WIDTH *2/ 5, HEIGHT * 7 / 8, WIDTH / 5, HEIGHT / 16);
[button setTitle:@"點(diǎn)擊進(jìn)入" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.layer.borderWidth = 2;
button.layer.cornerRadius = 5;
button.clipsToBounds = YES;
button.layer.borderColor = [UIColor blackColor].CGColor;
[button addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:button];
}
imageView.image = image;
[myScrollView addSubview:imageView];
}
myScrollView.bounces = NO;
myScrollView.pagingEnabled = YES;
myScrollView.showsHorizontalScrollIndicator = NO;
myScrollView.contentSize = CGSizeMake(WIDTH * 4, HEIGHT);
myScrollView.delegate = self;
[self.view addSubview:myScrollView];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, HEIGHT * 15 / 16, WIDTH / 3, HEIGHT / 16)];
pageControl.centerX = WIDTH/2;
// 設(shè)置頁數(shù)
pageControl.numberOfPages = 4;
// 設(shè)置頁碼的點(diǎn)的顏色
pageControl.pageIndicatorTintColor = RGBA(187, 192, 191, 1);
// 設(shè)置當(dāng)前頁碼的點(diǎn)顏色
pageControl.currentPageIndicatorTintColor = RGBA(78, 170, 155, 1);
[self.view addSubview:pageControl];
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// 計(jì)算當(dāng)前在第幾頁
pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
}
// 點(diǎn)擊按鈕保存數(shù)據(jù)并切換根視圖控制器
- (void) go:(UIButton *)sender{
flag = YES;
NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
// 保存用戶數(shù)據(jù)
[useDef setBool:flag forKey:@"notFirst"];
[useDef synchronize];
// 發(fā)送通知, 修改根視圖控制器? to? appdelegate
[[NSNotificationCenter defaultCenter] postNotificationName:@"rootVC" object:nil];
}
當(dāng)引導(dǎo)頁展示完成后, 修改根視圖控制器, 這樣就ok了.