ios-第一次启动APP的引导页、登录页、首页
APPdelegate
// // AppDelegate.m #import "AppDelegate.h" #import "RootViewController.h" //根控制器 #import "WelcomeViewController.h" //引导页 #import "LoginViewController.h"//登录页 @interface AppDelegate () { RootViewController *rootVC; } @property (nonatomic,strong) WelcomeViewController *welcomeVC; @property (nonatomic,strong) LoginViewController *loginVC; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; self.window.backgroundColor = WhiteColor; [self createRooVC]; [self chooseRootVC]; [self.window makeKeyAndVisible]; return YES; } -(WelcomeViewController *)welcomeVC { if (!_welcomeVC) { _welcomeVC = [[WelcomeViewController alloc]init]; __weak typeof(self) weakSelf = self; //引导页的回调 _welcomeVC.callBack = ^(){ [weakSelf chooseRootVC]; }; } return _welcomeVC; } -(void)createRooVC { rootVC = [RootViewController defaultsTabBar];; } //选择 rootViewController -(void)chooseRootVC { BOOL flag = [[NSUserDefaults standardUserDefaults]boolForKey:@"firstIn"]; if (flag == YES) { self.window.rootViewController = rootVC; } else { self.window.rootViewController = self.loginVC; } } -(LoginViewController *)loginVC{ if (!_loginVC) { _loginVC = [[LoginViewController alloc]init]; __weak typeof(self) weakSelf = self; //登录页的回调 _loginVC.callBack = ^(){ [weakSelf chooseRootVC]; }; } return _loginVC; } @end
RootViewController.h
#import <UIKit/UIKit.h> @interface RootViewController : UITabBarController +(id)defaultsTabBar; + (void)deallocTabbar; -(void)setTabBarHidden:(BOOL)Bool;//判断是否隐藏 底部标签栏 @end
RootViewController.m
// // RootViewController.m #import "RootViewController.h" #import "HomeViewVC.h" #import "NewViewVC.h" #import "UserViewVC.h" #import "LoginViewController.h" #define threeWidth Main_Width/3 static RootViewController *rootVC; @interface RootViewController () @property (nonatomic) BOOL tabBarIsShow; @property (nonatomic,strong) NSMutableArray *buttonArray; @property (nonatomic,strong) UIImageView *backImageView; @property (nonatomic,strong) NSMutableArray * labelArray; @end @implementation RootViewController + (void)deallocTabbar { rootVC = nil; } +(id)defaultsTabBar { if (rootVC == nil) { rootVC = [[RootViewController alloc]init]; } return rootVC; } - (void)viewDidLoad { [super viewDidLoad]; self.buttonArray = [NSMutableArray array]; self.labelArray = [NSMutableArray array]; //自己的tabBar self.tabBarIsShow = YES; //系统的tabBar self.tabBar.hidden = YES; self.view.backgroundColor = WhiteColor; HomeViewVC *homeVC = [[HomeViewVC alloc]init]; DefineNavigation *homeNC = [[DefineNavigation alloc]initWithRootViewController:homeVC]; NewViewVC *NewVC = [[NewViewVC alloc]init]; DefineNavigation *NewNC = [[DefineNavigation alloc]initWithRootViewController:NewVC]; UserViewVC *UserVC = [[UserViewVC alloc]init]; DefineNavigation *UserNC = [[DefineNavigation alloc]initWithRootViewController:UserVC]; self.viewControllers = @[homeNC,NewNC,UserNC]; [self setCustomTabbarItem]; } -(void)setCustomTabbarItem { self.backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, Main_height - 48*kHeight, Main_Width, 48*kHeight)]; UIImageView * imageline = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Main_Width, 0.5*kHeight)]; imageline.backgroundColor = Home_back_Color; [self.backImageView addSubview:imageline]; self.backImageView.backgroundColor = [UIColor whiteColor]; // self.backImageView.image = [UIImage imageNamed:@"wireframe_bottom"]; self.backImageView.userInteractionEnabled = YES; [self.view addSubview:self.backImageView]; NSArray *titleArray = @[@"首页", @"消息", @"个人中心"]; CGFloat space = Main_Width / 3.0; for (int i = 0; i < titleArray.count; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; NSString * imageNormal = [NSString stringWithFormat:@"home_bottom_%d0",i+1]; NSString * imageSelected =[NSString stringWithFormat:@"home_bottom_%d1",i+1]; [button setImage:[UIImage imageNamed:imageNormal] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:imageSelected] forState:UIControlStateSelected]; button.tag = 1001+i+100; UIButton * buttonBig = [UIButton buttonWithType:UIButtonTypeCustom]; buttonBig.tag = 1001+i; [buttonBig addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside]; [self.backImageView addSubview:button]; [button mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.backImageView.mas_top).with.offset(8*kHeight); make.left.mas_equalTo(self.backImageView.mas_left).with.offset(40*kWidth+i*space); make.size.mas_equalTo(CGSizeMake(24*kWidth, 24*kHeight)); }]; [self.backImageView addSubview:buttonBig]; [buttonBig mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.backImageView.mas_top).with.offset(8*kHeight); make.left.mas_equalTo(self.backImageView.mas_left).with.offset(threeWidth*i); make.size.mas_equalTo(CGSizeMake(threeWidth, 46*kHeight)); }]; UILabel * label = [self createUIlabel:titleArray[i] andFont:FontOfSize13 andColor:BlackColor]; label.text = titleArray[i]; if (i == 0) { button.selected = YES; [button setTitleColor:BlackColor forState:UIControlStateNormal]; label.textColor = GreedTitleColor; } [self.backImageView addSubview:label]; [label mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(button.mas_bottom).with.offset(1*kHeight); make.centerX.mas_equalTo(button.mas_centerX); }]; [self.labelArray addObject:label]; [self.buttonArray addObject:button]; } } -(void)selectButtonWithIndex:(NSInteger)index { UIButton *btn = (UIButton *)self.buttonArray[index]; [self pressButton:btn]; } -(void)setTabBarHidden:(BOOL)Bool { if (Bool) { [self hideTabBar]; } else { [self showTabBar]; } } //隐藏 tabBar(y坐标变大) -(void)hideTabBar { if (!self.tabBarIsShow) { return; } [UIView animateWithDuration:0.35 animations:^{ self.backImageView.frame = CGRectMake(0, Main_height, Main_Width, 48*kHeight); }]; self.tabBarIsShow = NO; } //显示 tabBar(y坐标变小) -(void)showTabBar { if (self.tabBarIsShow) { return; } [UIView animateWithDuration:0.35 animations:^{ self.backImageView.frame = CGRectMake(0, Main_height - 48*kHeight, Main_Width, 48*kHeight); }]; self.tabBarIsShow = YES; } -(void)pressButton:(UIButton *)sender { if (sender.tag - 1001 >= 4) { return; } //一定要有这个 self.selectedIndex = sender.tag - 1001; for (UILabel * label in self.labelArray) { label.textColor = littleBlackColor; } UILabel * label = self.labelArray[self.selectedIndex]; label.textColor = GreedTitleColor; NSInteger smallBtn = sender.tag + 100; for (UIButton *tempBtn in self.buttonArray) { if (tempBtn.tag == smallBtn) { tempBtn.selected = YES; [tempBtn setTitleColor:BlackColor forState:UIControlStateNormal]; } else { tempBtn.selected = NO; [tempBtn setTitleColor:GrayColor forState:UIControlStateNormal]; } } } //判断是否跳转到 登录界面 -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *string = [defaults objectForKey:@"login"]; if (string == nil) { LoginViewController *loginVC = [[LoginViewController alloc]init]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self presentViewController:loginVC animated:YES completion:nil]; }); } } -(UILabel*)createUIlabel:(NSString *)title andFont:(CGFloat)font andColor:(UIColor*)color { UILabel * label =[[UILabel alloc]init]; label.text = title; label.textColor = color; UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:font];CGSize size = [label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]]; CGFloat nameH = size.height; CGFloat nameW = size.width; label.textAlignment = NSTextAlignmentCenter; label.frame =CGRectMake(0, 0, nameW, nameH); label.font = Font(font); return label; } @end
WelcomeViewController.h
#import <UIKit/UIKit.h> @interface WelcomeViewController : UIViewController //回调用 @property (nonatomic,strong) void (^callBack)(void); @end
WelcomeViewController.m
// // WelcomeViewController.m #import "WelcomeViewController.h" #import "LoginViewController.h" @interface WelcomeViewController () @property (nonatomic,strong) UIScrollView *scrollView; @end @implementation WelcomeViewController - (void)viewDidLoad { [super viewDidLoad]; self.scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds]; for (int i =0; i<4; i++) { UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(Main_Width * i, 0, Main_Width, Main_height)]; NSString * nameStr = [[NSString alloc]initWithFormat:@"qidong%d.png",i+1]; imageView.image = [UIImage imageNamed:nameStr]; //最后一张图片 添加 点击按钮 if (i==3) { UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setTitle:@"" forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@"unpress"] forState:UIControlStateNormal]; btn.tag = 100; [btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside]; [btn setImage:[UIImage imageNamed:@"press"] forState:UIControlStateHighlighted]; imageView.userInteractionEnabled = YES; [imageView addSubview:btn]; [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(imageView.mas_centerX); make.bottom.equalTo(imageView.mas_bottom).with.offset(-77*kHeight); make.size.mas_equalTo(CGSizeMake(160*kWidth, 38*kHeight)); }]; } [self.scrollView addSubview:imageView]; } self.scrollView.contentOffset = CGPointMake(0, 0); self.scrollView.contentSize = CGSizeMake(Main_Width * 4, Main_height); self.scrollView.bounces = NO; self.scrollView.pagingEnabled = YES; self.scrollView.showsHorizontalScrollIndicator = NO; self.scrollView.showsVerticalScrollIndicator = NO; [self.view addSubview:self.scrollView]; } -(void)pressBtn { //保存为已经进入app [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"firstIn"]; [[NSUserDefaults standardUserDefaults]synchronize];//及时保存 不等到按home键就保存内容 //延迟执行 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if (self.callBack) { self.callBack(); }; }); } @end
LoginViewController.h
#import <UIKit/UIKit.h> @interface LoginViewController : UIViewController @property (nonatomic,strong) void (^callBack)(void); @end
LoginViewController.m
// // LoginViewController.m #import "LoginViewController.h" #import "ForgetpasswordController.h" @interface LoginViewController ()<UITextFieldDelegate> { UIView * nav; } @property (nonatomic,strong) UITextField *phoneTextField; @property (nonatomic,strong) UITextField *captchaTextField; @property (nonatomic,strong) UIButton *captchaButton; @property (nonatomic,strong) UIScrollView *scrollView; @property (nonatomic,strong) WKProgressHUD *hud; @property (nonatomic,copy) NSString *uuidString; @end @implementation LoginViewController - (void)viewDidLoad { [super viewDidLoad]; nav = [self createNavView]; [self createNavTitle:@"登录"]; [self.view addSubview:nav]; [self.view addSubview:self.scrollView]; [self initSubviews]; } - (UIScrollView *)scrollView { if (!_scrollView) { _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Main_Width, Main_height)]; _scrollView.contentSize = CGSizeMake(Main_Width, Main_height); _scrollView.backgroundColor = WhiteColor; _scrollView.scrollEnabled = NO; } return _scrollView; } -(void)initSubviews{ UIImageView * backImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"login"]]; backImage.frame =CGRectMake(0, -20, Main_Width, Main_height); [self.scrollView addSubview:backImage]; ///顶部Logo UIImageView *logoImageV = [[UIImageView alloc]initWithFrame:CGRectMake((Main_Width - 108)/2.0, 55 * kHeight, 108, 40)]; logoImageV.image = [UIImage imageNamed:@"register_logo"]; //输入框 self.phoneTextField = [UITextField createField:@"请输入手机号" andFont:FontOfSize12 andTextColor:WhiteColor andNSColor:UITextColor]; self.phoneTextField.delegate = self; self.captchaTextField = [UITextField createField:@"请输入6-14位密码" andFont:FontOfSize12 andTextColor:WhiteColor andNSColor:UITextColor]; self.captchaTextField.delegate = self; self.captchaTextField.secureTextEntry = YES; //手机号 UIView *firstV = [Common backViewWithFloat:(200 *kHeight) andField:self.phoneTextField andTitle:@"账号"]; [self.scrollView addSubview:firstV]; [firstV mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.scrollView.mas_centerX); make.size.mas_equalTo(CGSizeMake(273*kWidth, cellHeight)); make.top.mas_equalTo(self.scrollView.mas_top).with.offset(216 *kHeight); }]; //密码 UIView *secondV = [Common backViewWithFloat:CGRectGetMaxY(firstV.frame)+20.0*kHeight andField:self.captchaTextField andTitle:@"密码"]; [self.scrollView addSubview:secondV]; [secondV mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.scrollView.mas_centerX); make.size.mas_equalTo(CGSizeMake(273*kWidth, cellHeight)); make.top.mas_equalTo(firstV.mas_bottom).with.offset(20 *kHeight); }]; ///登录 UIButton *loginButton = [UIButton createUIButton:@"登录" andFont:FontOfSize16 andBackGroundColor:GreedWhiteColor andTitleColor:WhiteColor andControllerVC:self andAction:@selector(LoginButtonAction)]; loginButton.frame = CGRectMake( 18,CGRectGetMaxY(secondV.frame) + 52 * kHeight, Main_Width - 36, Button_Height); LRViewBorderRadius(loginButton, cellHeight/2, 0.5*kWidth, WhiteColor); [self.scrollView addSubview:loginButton]; [loginButton mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.scrollView.mas_centerX); make.size.mas_equalTo(CGSizeMake(273*kWidth, cellHeight)); make.top.mas_equalTo(secondV.mas_bottom).with.offset(52 *kHeight); }]; ///忘记密码 UIButton *forgetButton = [UIButton createUIButton:@"忘记密码?" andFont:FontOfSize14 andBackGroundColor:nil andTitleColor:GreedBlackColor andControllerVC:self andAction:@selector(forgetButton)]; forgetButton.frame = CGRectMake(0, CGRectGetMaxY(loginButton.frame) + 15, 110, 20); [self.scrollView addSubview:forgetButton]; [forgetButton mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.scrollView.mas_centerX); make.top.mas_equalTo(loginButton.mas_bottom).with.offset(26*kHeight); make.size.mas_equalTo(CGSizeMake(110*kWidth, 20*kHeight)); }]; /* ///注册 UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom]; registerButton.frame = CGRectMake(screenWidth - 20 - 40, CGRectGetMaxY(loginButton.frame) + 15, 40, 20); [registerButton setTitleColor:YellowColor forState:UIControlStateNormal]; [registerButton setTitle:@"注册" forState:UIControlStateNormal]; registerButton.titleLabel.font = Font(13); [registerButton addTarget:self action:@selector(registerButtonAction) forControlEvents:UIControlEventTouchUpInside]; [self.scrollView addSubview:registerButton]; */ UILabel * bottomTitle = [UILabel createUIlabel:@"北京©版权所有" andFont:FontOfSize13 andColor:GreedBlackColor]; [self.scrollView addSubview:bottomTitle]; [bottomTitle mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.scrollView.mas_centerX); make.top.mas_equalTo(forgetButton.mas_bottom).with.offset(65*kHeight); }]; } #pragma mark 登录方法 -(void)LoginButtonAction{ //保存为已经进入app [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"firstIn"]; [[NSUserDefaults standardUserDefaults]synchronize];//及时保存 不等到按home键就保存内容 [[NSUserDefaults standardUserDefaults] setObject:@"login" forKey:@"login"]; //获取uuid [self sendUUID]; NSDictionary *dic = @{@"loginName" : self.phoneTextField.text, @"pwd": self.captchaTextField.text}; NSArray *arr = [dic allValues]; if ([arr containsObject:@""]) { [WKProgressHUD popMessage:@"请将信息补充完整" inView:self.view duration:1.5 animated:YES]; return; } __weak typeof(self) weakSelf = self; [WKProgressHUD showInView:self.view withText:@"" animated:YES]; if (self.callBack) { _callBack(); }else{ [self.navigationController dismissViewControllerAnimated:YES completion:nil]; }; } #pragma mark 忘记密码 - 推出忘记密码Vc - (void)forgetButton{ ForgetpasswordController *forgetVC = [[ForgetpasswordController alloc]init]; [self.navigationController pushViewController:forgetVC animated:YES]; } #pragma mark - 获取UUID - (void)sendUUID{ CFUUIDRef uuid = CFUUIDCreate(NULL); assert(uuid != NULL); CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid); if (![SSKeychain passwordForService:@"com.sandwish.eduOnline" account:@"user"]) { //查看本地是否存储指定 serviceName 和 account 的密码 [SSKeychain setPassword: [NSString stringWithFormat:@"%@", uuidStr] forService:@"com.sandwish.eduOnline"account:@"user"]; //从钥匙串读取UUID: NSString *retrieveuuid = [SSKeychain passwordForService:@"com.sandwish.eduOnline"account:@"user" ]; //NSLog(@"SSKeychain存储显示 :第一次安装过:%@", retrieveuuid); self.uuidString = [NSString stringWithFormat:@"%@",retrieveuuid]; } else { //曾经安装过 则直接能打印出密码信息(即使删除了程序 再次安装也会打印密码信息) 区别于 NSUSerDefault //从钥匙串读取UUID: NSString *retrieveuuid = [SSKeychain passwordForService:@"com.sandwish.eduOnline" account:@"user"]; NSLog(@"SSKeychain存储显示 :已安装过:%@", retrieveuuid); self.uuidString = [NSString stringWithFormat:@"%@",retrieveuuid]; } } #pragma mark - UITextField协议 - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } - (void)textFieldDidBeginEditing:(UITextField *)textField { if (textField == self.captchaTextField) { [UIView animateWithDuration:0.3 animations:^{ /* 计算偏移量 */ CGFloat height = 216; UIButton *button = (UIButton *)[self.view viewWithTag:400]; CGFloat offsetHeight = CGRectGetMaxY(button.frame) - (Main_height - height - 64); if (offsetHeight > 0) { self.scrollView.contentInset = UIEdgeInsetsMake(-offsetHeight , 0, 0, 0); } } completion:nil]; } } - (void)textFieldDidEndEditing:(UITextField *)textField{ [UIView animateWithDuration:0.2 animations:^{ self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); }]; } @end