iOS开发笔记--应用首次启动显示用户引导

本文介绍了一种iOS应用中实现首次启动用户引导页的方法。通过创建自定义的UserGuideViewController并利用UIScrollView来展示一系列图片,实现了流畅的分页浏览效果。同时,通过NSUserDefaults记录应用是否为首次启动,确保引导页只在初次使用时展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个功能的重点就是在如何判断应用是第一次启动的. 其实很简单

我们只需要在一个类里面写好用户引导页面  基本上都是使用UIScrollView 来实现,

新建一个继承于UIViewController的类 命名为 UserGuideViewController ,

UserGuideViewController.m 写

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    
    [self initGuide];   //加载新用户指导页面
}

- (void)initGuide
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 640)];
    [scrollView setContentSize:CGSizeMake(1280, 0)];
    [scrollView setPagingEnabled:YES];  //视图整页显示
    //    [scrollView setBounces:NO]; //避免弹跳效果,避免把根视图露出来
    
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    [imageview setImage:[UIImage imageNamed:@"0.png"]];
    [scrollView addSubview:imageview];
    [imageview release];
    
    UIImageView *imageview1 = [[UIImageView alloc] initWithFrame:CGRectMake(320, 0, 320, 460)];
    [imageview1 setImage:[UIImage imageNamed:@"1.png"]];
    [scrollView addSubview:imageview1];
    [imageview1 release];
    
    UIImageView *imageview2 = [[UIImageView alloc] initWithFrame:CGRectMake(640, 0, 320, 460)];
    [imageview2 setImage:[UIImage imageNamed:@"2.png"]];
    [scrollView addSubview:imageview2];
    [imageview2 release];
    
    UIImageView *imageview3 = [[UIImageView alloc] initWithFrame:CGRectMake(960, 0, 320, 460)];
    [imageview3 setImage:[UIImage imageNamed:@"3.png"]];
    imageview3.userInteractionEnabled = YES;    //打开imageview3的用户交互;否则下面的button无法响应
    [scrollView addSubview:imageview3];
    [imageview3 release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//在imageview3上加载一个透明的button
    [button setTitle:nil forState:UIControlStateNormal];
    [button setFrame:CGRectMake(46, 371, 230, 37)];
    [button addTarget:self action:@selector(firstpressed) forControlEvents:UIControlEventTouchUpInside];
    [imageview3 addSubview:button];
    
    [self.view addSubview:scrollView];
    [scrollView release];
}

button的方法

- (void)firstpressed
 {
     [self presentModalViewController:[[[WeiBoViewController alloc] init] autorelease] animated:YES];  //点击button跳转到根视图
 }

至于添加button是因为我的用户引导最后一个页面有一个画上去的button,写着 开始使用  我在上面添加一个透明的button 用以实现调用方法

 

打开AppDelegate.m

首先引入头文件

 #import "UserGuideViewController.h"
 #import "WeiBoViewController.h"

WeiBoViewController是我的根视图

application: didFinishLaunchingWithOptions: 方法内进行判断

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    
    //判断是不是第一次启动应用
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        NSLog(@"第一次启动");        
        //如果是第一次启动的话,使用UserGuideViewController (用户引导页面) 作为根视图
        UserGuideViewController *userGuideViewController = [[UserGuideViewController alloc] init];
        self.window.rootViewController = userGuideViewController;
        [userGuideViewController release];
    }
    else
    {
        NSLog(@"不是第一次启动");
        //如果不是第一次启动的话,使用LoginViewController作为根视图
        WeiBoViewController *weiBoViewController = [[WeiBoViewController alloc] init];
        self.window.rootViewController = weiBoViewController;
        [weiBoViewController release];

    }
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

这样,就可以实现第一次打开应用显示用户引导 之后不再显示
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值