iOS开发 首次启动显示用户引导,第二次启动直接进入App

Posted 徒步天涯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS开发 首次启动显示用户引导,第二次启动直接进入App相关的知识,希望对你有一定的参考价值。

首先创建一个引导图的控制器类

UserGuideViewController.h和UserGuideViewController.m

#import <UIKit/UIKit.h>
#import "firstViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface UserGuideViewController : UIViewController<UIScrollViewDelegate>

@end
#import "UserGuideViewController.h"

@interface UserGuideViewController ()
@property(strong,nonatomic)UIScrollView *scrollView;
@property(strong,nonatomic)UIPageControl *page;
@property(strong,nonatomic)UIImageView *image1;
@property(strong,nonatomic)UIImageView *image2;
@property(strong,nonatomic)UIImageView *image3;
@property(strong,nonatomic)UIButton *btn;
@end

@implementation UserGuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //加载用户引导图
    [self initScroll];

}
-(void)initScroll{
    self.image1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.image1.image=[UIImage imageNamed:@"1"];
    
    self.image2=[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT)];
    self.image2.image=[UIImage imageNamed:@"2"];
    
    self.image3=[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH*2, 0, WIDTH, HEIGHT)];
    self.image3.image=[UIImage imageNamed:@"3"];
    
    self.scrollView=[[UIScrollView alloc]initWithFrame:self.view.frame];
    self.scrollView.backgroundColor=[UIColor redColor];
    self.scrollView.contentSize=CGSizeMake(WIDTH*3, HEIGHT);
    
    //锁定滚动方向
    self.scrollView.directionalLockEnabled=YES;
    //设置分页
    self.scrollView.pagingEnabled=YES;
    //隐藏滚动条
    self.scrollView.showsHorizontalScrollIndicator=NO;
    //设置是否回弹
    self.scrollView.bounces=NO;
    
    //添加按钮
    self.btn=[[UIButton alloc]initWithFrame:CGRectMake(80, 600, 230, 37)];
        [self.btn setTitle:@"立即体验" forState:0];
    self.btn.titleLabel.font=[UIFont boldSystemFontOfSize:20];
    [self.btn setTitleColor:[UIColor colorWithRed:1.000 green:0.886 blue:0.107 alpha:1.000] forState:0];
    self.btn.backgroundColor=[UIColor redColor];
    [self.btn addTarget:self action:@selector(firstpressed) forControlEvents:UIControlEventTouchUpInside];

    [self.image3 addSubview:self.btn];
    
    //设置分页各个属性
    self.page=[[UIPageControl alloc]init];
    CGSize pageSize=CGSizeMake(120, 44);
    self.page.frame=CGRectMake((WIDTH-pageSize.width)*0.5, HEIGHT-pageSize.height-40, pageSize.width, pageSize.height);
    
    
    self.page.backgroundColor=[UIColor clearColor];
    //设置分页页数
    self.page.numberOfPages=3;
    self.page.currentPage=0;
    
    
    self.scrollView.delegate=self;
    
    [self.scrollView addSubview:self.image1];
    [self.scrollView addSubview:self.image2];
    [self.scrollView addSubview:self.image3];
    
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.page];
    //打开用户交互,否则下面的button无法响应
    self.image3.userInteractionEnabled=YES;

}
//按钮的处罚时间
-(void)firstpressed{
    //跳转至正文
    
    [self presentViewController:[firstViewController new] animated:YES completion:^{
        
    }];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //设置分页
    
    self.page.currentPage=(int)(scrollView.contentOffset.x/WIDTH);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

正文页firstViewController.h和firstViewController.m

#import <UIKit/UIKit.h>

@interface firstViewController : UIViewController

@end
#import "firstViewController.h"
@interface firstViewController ()

@end

@implementation firstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor redColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

AppDelegate.mAppDelegate.h文件

#import <UIKit/UIKit.h>
#import "firstViewController.h"
#import "UserGuideViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property(strong,nonatomic)firstViewController *firstVc;
@property (strong, nonatomic) UIWindow *window;


@end
#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.firstVc=[[firstViewController alloc]init];
    
    //判断应用是否是第一次启动
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"firstLaunch"];
        NSLog(@"第一次启动");
        //如果是第一次启动的话,使用UserGuideViewController(用户引导页面) 作为根视图
        UserGuideViewController *userViewController=[[UserGuideViewController alloc]init];
        self.window.rootViewController=userViewController;
    }else{
        NSLog(@"不是第一次启动");
        //如果不是第一次启动的话,使用first作为根视图
        firstViewController *first=[[firstViewController alloc]init];
        self.window.rootViewController=first;
    
    
    }
    self.window.backgroundColor=[UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

运行效果

第一次运行

技术分享

第二次运行

技术分享

 

以上是关于iOS开发 首次启动显示用户引导,第二次启动直接进入App的主要内容,如果未能解决你的问题,请参考以下文章

首次用户体验的启动屏幕看起来不正确 (iOS)

应用程序第二次启动时显示第二个 ViewController

iOS引导页启动页

iOS引导页启动页

应用程序第二次启动时显示不同的视图

在第二次启动后显示 UIAlertView