美文网首页Object-CiOS开发交流平台good
iOS启动页面和引导页面的实现

iOS启动页面和引导页面的实现

作者: 马之赛克 | 来源:发表于2016-04-28 00:23 被阅读4736次

这几天有点事多,都没时间摸电脑,几天不学习都不知道写代码了,话不多说,直接上代码,难免逻辑有bug,还请各位大神不要见笑,还望不吝赐教

程序欢迎界面我做的很不美,临时下载了几张图


首先创建两个文件welcomeVC和firstVC分别是启动页面和引导页面都继承自UIViewController


首先是welcomeVC.h

#import<UIKit/UIKit.h>

#define WIDTH (NSInteger)self.view.bounds.size.width

#define HEIGHT (NSInteger)self.view.bounds.size.height

@interface firstVC : UIViewController

@end

然后是welcomeVC.m

#import "welcomeVC.h"

#import "firstVC.h"

@interface welcomeVC ()

@property(nonatomic,strong)UIView*lunchV;

@property(nonatomic,strong)UIImageView*imageV;

@end

@implementation welcomeVC

- (void)viewDidLoad {

[super viewDidLoad];

_lunchV = [[UIView alloc] init];

self.lunchV.frame = CGRectMake(0, 0, WIDTH, HEIGHT);

[self.view addSubview:_lunchV];

UIImageView*imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];

UIImage*image = [UIImage imageNamed:@"1.JPG"];

imageV.image = image;

[self.lunchV addSubview:imageV];

[self.view bringSubviewToFront:self.lunchV];

[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(removelunch) userInfo:nil repeats:NO];

[NSTimer scheduledTimerWithTimeInterval:3.1 target:self selector:@selector(change) userInfo:nil repeats:NO];

}

-(void)removelunch

{

[self.lunchV removeFromSuperview];

}

- (void)change

{

self.view.window.rootViewController = [[firstVC alloc] init];

}

其中有些地方我自己不是很确定逻辑是否合理,还请不要见怪

firstVC.h

#import <UIKit/UIKit.h>

#define WIDTH (NSInteger)self.view.bounds.size.width

#define HEIGHT (NSInteger)self.view.bounds.size.height

@interface firstVC : UIViewController

@end

firstVC.m文件


#import "ViewController.h"

#import "firstVC.h"

@interface firstVC (){

// 创建页码控制器

UIPageControl *pageControl;

// 判断是否是第一次进入应用

BOOL flag;

}

@end

@implementation firstVC

- (void)viewDidLoad {

[super viewDidLoad];

UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

for (int i=0; i<3; i++)

{

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"1-%d.jpg",i+1]];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];

// 在最后一页创建按钮

if (i == 2)

{

// 必须设置用户交互 否则按键无法操作

imageView.userInteractionEnabled = YES;

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

button.frame = CGRectMake(WIDTH / 3, HEIGHT * 7 / 8, WIDTH / 3, HEIGHT / 16);

[button setTitle:@"点击进入" forState:UIControlStateNormal];

[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

button.layer.borderWidth = 2;

button.layer.cornerRadius = 5;

button.clipsToBounds = YES;

button.layer.borderColor = [UIColor whiteColor].CGColor;

[button addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];

[imageView addSubview:button];

}

imageView.image = image;

[myScrollView addSubview:imageView];

}

myScrollView.bounces = NO;

myScrollView.pagingEnabled = YES;

myScrollView.showsHorizontalScrollIndicator = NO;

myScrollView.contentSize = CGSizeMake(WIDTH * 3, HEIGHT);

myScrollView.delegate = self;

[self.view addSubview:myScrollView];

pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 3, HEIGHT * 15 / 16, WIDTH / 3, HEIGHT / 16)];

// 设置页数

pageControl.numberOfPages = 3;

// 设置页码的点的颜色

pageControl.pageIndicatorTintColor = [UIColor yellowColor];

// 设置当前页码的点颜色

pageControl.currentPageIndicatorTintColor = [UIColor redColor];

[self.view addSubview:pageControl];

}

#pragma mark - UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

// 计算当前在第几页

pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);

}

// 点击按钮保存数据并切换根视图控制器

- (void) go:(UIButton *)sender{

flag = YES;

NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];

// 保存用户数据

[useDef setBool:flag forKey:@"notFirst"];

[useDef synchronize];

// 切换根视图控制器

self.view.window.rootViewController = [[ViewController alloc] init];

}

AppDelegate.h文件不做变动

AppDelegate.m文件

#import "AppDelegate.h"

#import "welcomeVC.h"

#import "firstVC.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[self.window makeKeyAndVisible];

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

self.window.backgroundColor = [UIColor whiteColor];

welcomeVC*root = [[welcomeVC alloc] init];

self.window.rootViewController = root;

if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];

NSLog(@"首次启动");

UIViewController *vc = [[UIViewController alloc] init];

self.window.rootViewController = vc;

}else {

NSLog(@"非首次启动");

welcomeVC *vc1 = [[welcomeVC alloc] init];

self.window.rootViewController = vc1;

}

return YES;

}

- (void)change

{

self.window.rootViewController = [[firstVC alloc] init];

}

ViewController.h和ViewController.m文件我没有写代码也就是主界面了



我自己搜了好多文档也浏览了不少博客,东拼西凑的总算达到了我要的效果,我知道其中有不合理的地方,还请各位大牛多多指点,或许是我自己学习方法不对吧,一个人看博客泡论坛,看视频,自学速度不是很快,但是还是有那么些许收获的,这就是对我的付出的最大回报,

看了很多大牛说,学习最快的办法就是输出,输出就是把书本的只是,把自己学到的知识,通过思考转化为自己的东西,最快的转化的方法就是拿出来和别人一起探讨,交流。所以在这里,对于这一篇东拼西凑才打到效果的文章,我就不写那些原著的大牛了,请各位朋友多多指教

相关文章

  • iOS启动页面和引导页面的实现

    这几天有点事多,都没时间摸电脑,几天不学习都不知道写代码了,话不多说,直接上代码,难免逻辑有bug,还请各位大神不...

  • 关于iOS应用启动页与引导页的显示切换

    我们在移动应用开发中经常会应用到启动页与引导页,为了实现启动页与引导页,以及应用功能界面的无缝连接,今天我...

  • 产品交互自查笔记#03启动页、引导页、登录页

    前言:上一篇讲了页面的打开方式,里面提到当首次打开应用时,会出现的启动页、引导页或登录页,当然启动页和登录页不一定...

  • Flutter 本地存储值引导页和主页面切换

    在移动开发中,第一次安装应用时,引导页是必不可少的, 这里主要操作时,引导页和主页面的切换状态显示问题的实现, 本...

  • iOS引导页、启动页

    前言 这里使用 launchScreen 、.storyboard 文件创建启动图和引导页。首次打开项目或者更新后...

  • Flutter版本的玩Android完整版本(终结)

    用Flutter开发的跨平台项目,完美运行在Android和IOS上,Material简洁风格,包括启动页、引导页...

  • iOS启动时实现引导页面

    学习了下引导页的实现方法,如果设计到了版权或者抄袭问题请联系我删除,本文只作为我学习记录一下。好了让我们直接上效果...

  • App页面分类

    引导页(欢迎页) 过渡页(启动页) 加载页 沉浸式页面 功能页,eg: 登陆、注册,设置,发布… 列表页 正文页,...

  • Android 引导页

    引导页 思路1.判断当前系统版本号2.如果与保存版本号不相同启动引导页跳转主页面如果相同等待指定时间跳转启动页 创...

  • iOS适配启动页

    iOS适配启动页 iOS适配启动页

网友评论

  • SilveryApril:这篇文章中,至少有三个值得优化的地方:
    第一:NSUserDefaults中点击引导页按钮时存的key值,和AppDelegate中取的值明显不同,你居然说能实现效果,真是不可思议;
    第二:welcomeVC中并没有对是否是第一次启动做判断,也就是说,只要加载了welcomeVC,后面就一定会加载firstVC;
    第三:AppDelegate中change方法,我不知道是你没完全写出来,还是思路没理清,总之我没看到在哪里会调用它。
    第四:welcomeVC真的有必要用两个定时器么?虽然我没有测试过,但是移除启动页视图和更换window根视图,感觉没什么冲突,完全没有必要把一个方法里能做的事分到两个方法里去。
    代码之路遥遥无期,希望在这条路上都能互相帮助,共同进步。如果觉得我哪里说的不对,请指正。
  • Scorpio_糖果屋:为什么不用Markdown编辑呢?

本文标题:iOS启动页面和引导页面的实现

本文链接:https://www.haomeiwen.com/subject/dffilttx.html