1.新建一个WelcomeViewController
,代码如下
#import "WelcomeViewController.h"
#import "RootTabBar.h"
#import "LoginView.h"
@interface WelcomeViewController ()<UIScrollViewDelegate>
{
// 创建页码控制器
UIPageControl * pageControl;
// 判断是否是第一次进入应用
BOOL flag;
NSString * MyTokenID;
NSString * MyUserID;
}
@end
@implementation WelcomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self MakeUI];
}
- (void)MakeUI{
UIScrollView * myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
for (int i = 0; i < 3; i++) {
UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"welcome%d",i+1]];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
//最后一页创建按钮
if (i == 2) {
imageView.userInteractionEnabled = YES;
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(SCREEN_WIDTH/3, SCREEN_HEIGHT*7/8-20, SCREEN_WIDTH/3, 40);
[button setTitle:@"立即体验" forState:UIControlStateNormal];
[button setTitleColor:XHRGBA(91, 178, 187, 1) forState:UIControlStateNormal];
button.layer.borderColor = XHRGBA(91, 178, 187, 1).CGColor;
button.layer.borderWidth = 1;
button.layer.cornerRadius = SLLCornerRadius;
button.layer.masksToBounds = YES;
[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(SCREEN_WIDTH*3, SCREEN_HEIGHT);
myScrollView.delegate = self;
[self.view addSubview:myScrollView];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/3, SCREEN_HEIGHT*15/16-10, SCREEN_WIDTH/3, SCREEN_HEIGHT/16)];
pageControl.numberOfPages = 3;
pageControl.pageIndicatorTintColor = XHRGBA(206, 206, 206, 1);
pageControl.currentPageIndicatorTintColor = XHRGBA(91, 178, 187, 1);
[self.view addSubview:pageControl];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x/SCREEN_WIDTH);
}
- (void)Go:(UIButton *)sender{
flag = YES;
NSUserDefaults * userDef = [NSUserDefaults standardUserDefaults];
//保存用户数据
[userDef setBool:flag forKey:@"notFirst"];
[userDef synchronize];
//切换根视图控制器
[self GoToHomeView];
}
- (void) GoToHomeView
{
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
MyTokenID = [ user objectForKey:@"UserToken"];
MyUserID = [user objectForKey:@"UserID"];
if (MyTokenID.length == 0) {
LoginView *login = [[LoginView alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
self.view.window.rootViewController = nav;
}
else
{
RootTabBar *tabs = [[RootTabBar alloc] init];
self.view.window.rootViewController = tabs;
}
}
2,在AppDelegate.m文件中添加
if (![user boolForKey:@"notFirst"]) {
//第一次--进入启动页
self.window.rootViewController = [[WelcomeViewController alloc] init];
}else{
if (MyTokenID.length == 0) {
LoginView *login = [[LoginView alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
self.window.rootViewController = nav;
}
else
{
RootTabBar *tabs = [[RootTabBar alloc] init];
self.window.rootViewController = tabs;
}
}
注意,对应的key值(notFirst),一定不能不一样。
还要注意,NSUserDefaults赋值之后要[userDef synchronize];
网友评论