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

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

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

运行效果

第一次运行

第二次运行

时间: 2024-10-29 15:26:42

iOS开发 首次启动显示用户引导,第二次启动直接进入App,UIScrollView,UIPageControl,NSUserDefaults的相关文章

iOS开发之应用首次启动显示用户引导

这个功能的重点就是在如何判断应用是第一次启动的. 其实很简单 我们只需要在一个类里面写好用户引导页面  基本上都是使用UIScrollView 来实现, 新建一个继承于UIViewController的类 命名为 UserGuideViewController , 在UserGuideViewController.m 写 1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 // Do any additional setup after load

iOS开发-自定义后台显示图片(iOS7-Background Fetch的应用)

之前在用电池医生的时候, 发现它有这样一个功能:当应用进入后台的时候, 会显示另外一张图片覆盖App Switcher显示的界面. 效果如下: 变成----> 而这样的一个功能, 对于保护用户隐私还是挺有用的. 这就涉及到了Background Fetch的使用.当然, Background Fetch有更多,更有用的功能, 详见-->iOS 7学习:多任务处理之Background Fetch 下面就介绍下后台图片切换的实现. 1.程序配置后台模式 操作如下: 2. AppDelegate.

斯坦福大学IOS开发课程笔记(第七课第二部分)

转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/32163347 作者:小马 本篇是demo演示,程序其实就是上节课的心理学家那个demo, 不过在这个demo的基础上,把它作成一个通用版,可以自动识别当前运行的设备是iphone还是ipad,然后有不同的显示效果.所以,还是打开原来的Psychologist工程,在开始之前,我会用第五课讲的自动布局知识,让视图能在横屏模式下也可以正常显示.这一部分不细讲,参考第五课. 下面就可以

[iOS]如何在iOS开发中切换显示语言实现国际化

1.在Project设置,添加中英两种语言: 2.新建Localizable.strings文件,作为多语言对应的词典,存储多种语言,点击右侧Localization,勾选中英: 3.添加一个字段, 在English中,添加:"SUBMIT_BTN_TITLE" = "Go"; 在Chinese中,添加:"SUBMIT_BTN_TITLE" = "开始"; 4.一个工具类GDLocalizableController,用来切换

IOS开发之自动布局显示网络请求内容

在上一篇博客中详细的介绍了IOS开发中的相对布局和绝对布局,随着手机屏幕尺寸的改变,在App开发中为了适应不同尺寸的手机屏幕,用自动布局来完成我们想要实现的功能和效果显得尤为重要.本人更喜欢使用相对布局.在下面要学习的例子中暂且先用我们的StoryBoard来设置我们组件的约束,以后会在代码中给我们的元素新建约束.iPhone4,5和将要发布的iPhone6的屏幕的大小都不一样,所以屏幕的适配是我们搞App开发必须要考虑的问题. 我们要完成一个什么例子呢,先上两张程序运行最终的结果图,之后看着图

IOS开发——UI进阶篇(七)程序启动原理

一.Info.plist常见的设置 1.建立一个工程后,会在Supporting files文件夹下看到一个“工程名-Info.plist”的文件,该文件对工程做一些运行期的配置,非常重要,不能删除 在旧版本Xcode创建的工程中,这个配置文件的名字就叫“Info.plist” 项目中其他Plist文件不能带有“Info”这个字眼,不然会被错认为是传说中非常重要的“Info.plist” 项目中还有一个InfoPlist.strings的文件,跟Info.plist文件的本地化相关 2.常见属性

iOS开发之工具篇-20个可以帮你简化移动app开发流程的工具

本文转载至 http://www.cocoachina.com/ios/20130402/5926.html 如果想进入移动app开发这个领域,你总能从别的开发者或者网上或者书上找到各种各样的方法和工具,对于新手来说,还没有摸清门路就已经陷入迷茫了.这里推荐20个可以帮你简化app开发流程的工具.很多开发者都使用过这些工具,涉及原型和设计.编程.测试以及最后的营销,基本上涵盖了整个开发过程. 原型和设计 有了一个很好的创意后,你要做的不是立刻编程,而是设计UI和创建原型,这样你才能知道app如何

ios开发首次安装或者版本升级的引导页的判断

+ (BOOL) isShowUserGuideLoad{ NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary]objectForKey:@"CFBundleShortVersionString"];//version 1.0版本 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *lastRunVersio

iOS开发中——如何保存用户敏感信息(用户名和密码等信息)

使用Keychain存储用户敏感信息 iOS的keychain服务提供了一种安全的保存私密信息(密码,序列号,证书等)的方式,每个ios程序都有一个独立的keychain存储.相对于NSUserDefaults.文件保存等一般方式,keychain保存更为安全,而且keychain里保存的信息不会因App被删除而丢失,所以在重装App后,keychain里的数据还能使用.从ios 3.0开始,跨程序分享keychain变得可行. 如何需要在应用里使用使用keyChain,我们需要导入Securi