IOS程序启动引导示例

摘要

本章简述了IOS开发过程中程序第一次启动时的程序引导的示例,主要用到了UIScrollView作引导界面,使用NSUserDefaults相关键值判断程序是否第一次启动。


主要技术

判断是否第一次启动

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 是否第一次启动
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])
    {
        // 注意设置为TRUE
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        
        ViewController* viewController = [[ViewController alloc] init];
        viewController.firsttime = YES;
        self.window.rootViewController = viewController;
    }
    else
    {
        NSLog(@"不是第一次启动");
    }
    
    
    return YES;
}
此处注意,判断第一成功后把相关键值的值设置成了YES,使得下次不会再判断为YES,网上好多人都没有设置,但是我没设置就老是判断为是第一次启动,不知道其他人是什么情况。

同时,因为启动页面我是把他作为子页面放在程序的主页面里的,所以这里通过主页面控制器的属性来判断要不要显示引导页面,此处在判断为第一次启动后,将属性firsttime设置为YES,然后程序就会显示引导页面。


显示引导页面

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self initUI];
}

- (void)initUI
{
    // 启动动画
    if(self.firsttime)
    {
        [self showStartPage];
        self.firsttime = NO;
    }
    else
    {
        self.view.backgroundColor = [UIColor whiteColor];
        UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 350, 40)];
        label.text = @"欢迎你来到我的应用,我们在完善中...";
        [self.view addSubview:label];
    }
}

- (void)showStartPage
{
    NSInteger pageNumber = 3;
    
    // 滚动页
    UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width*pageNumber, 0)];
    scrollView.pagingEnabled = YES;
    [self.view addSubview:scrollView];
    _scrollView = scrollView;
    
    CGRect frame = self.view.frame;
    frame.origin.x -= frame.size.width;
    
    for (NSInteger i=0; i<pageNumber; i++)
    {
        frame.origin.x += frame.size.width;
        UIImageView* imageView = [[UIImageView alloc] initWithFrame:frame];
        [imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg", i+1]]];
        //[imageView setContentMode:UIViewContentModeScaleAspectFill];
        [scrollView addSubview:imageView];
    }
    
    CGFloat w = frame.size.width/2;
    CGFloat h = 40;
    CGFloat x = frame.origin.x + (frame.size.width-w)/2;
    CGFloat y = frame.origin.y+frame.size.height-60;
    
    UIButton* welcomeButton = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
    [welcomeButton setTitle:@"进入查看更多精彩" forState:UIControlStateNormal];
    [scrollView addSubview:welcomeButton];
    
    [welcomeButton addTarget:self action:@selector(onWelcome:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)onWelcome:(id)sender
{
    for (UIView* view in [_scrollView subviews])
    {
        [view removeFromSuperview];
    }
    [_scrollView removeFromSuperview];
    
    [self initUI];
}
当外面设置了 firsttime为YES时,会显示引导页面,此处将三幅图片作为引导页面示意的,使用控件UIScrollView管理。同时最后一个页面添加了一个按钮,点击该按钮即可进入程序主界面。


运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值