iOS 引导页

引导页

2016-04-23  本文已影响137人  箫声_筱昇

引导页

引导页是在程序第一次安装的时候呈现出来的画面.

#import "RootViewController.h"
#import "GuideViewController.h"

#define kUserDefaults [NSUserDefaults standardUserDefaults]
//定义长
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
//定义高
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
//自定义颜色变量.随机颜色
#define MYCOLOR(r,g,b,a) [UIColor colorWithRed:r green:g blue:b alpha:a]

在 AppDelegate.m中

//进行判断,如果是第一次打开此程序.则显示引导页.如果不是第一次打开,则直接显示程序页面
if(kUserDefaults boolForKey:@"isFirstLaunch"){
//根据键来判断,结果为 yes.说明不是首次加载.加载根式如控制器
RootViewController *rootVC = [[RootViewController alloc] init];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:rootVC];
}else{
//是首次加载,加载引导页.并且将 userDefaults 的值改为 yes.
GuideViewController *guideVC = [[GuideViewController alloc] init];
self.window.rootViewController = guideVC;
[kUserDefaults setBool:YES forKey:@"isFirstLaunch"];
}

在 GuideViewController.h中

#import "AppDelegate.n"
@interface GuideViewController ()
//声明一个 scrollview 的全局变量
@property (nonatomic, strong) UIScrollView *guideScrollView;
@end
- (void)viewDidLoad{
//初始化 scrollview. 页面大小为view.frame
 _guideScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
一面的偏移量.
 _guideScrollView.contentSize =CGSizeMake(kScreenWidth *3, 0);
    //分页效果
 _guideScrollView.pagingEnabled = YES;
    //隐藏指示条
_guideScrollView.showsHorizontalScrollIndicator = NO;
//添加子视图
for (int i = 0; i < 3; i++) {
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(i * kScreenWidth, 0, kScreenWidth, kScreenHeight)];
subView.backgroundColor = MYCOLOR(arc4random()% 256/255.0, arc4random()% 256/255.0, arc4random()% 256/255.0, 1.0);
if (i == 2) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setFrame:CGRectMake(100,500, 200, 50)];
[button setTitle:@"马上体验精彩生活" forState: UIControlStateNormal];
//添加回调方法.
[button addTarget:self action:@selector(huiddenGuiVC) forControlEvents:UIControlEventTouchUpInside];
//注意:button是要添加在subview 上的.
[subView addSubview:button];
        }
        [self.guideScrollView addSubview:subView];
  }
    //在 for 循环外部,将滚动视图添加到当前页面
    [self.view addSubview:self.guideScrollView];
}

#pragma mark-----huiddenGuiVC按钮的回调方法.
- (void)huiddenGuiVC{
    //置换 window 的根视图控制器.
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
appDelegate.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
    
}

上一篇下一篇

猜你喜欢

热点阅读