Object-CiOS开发交流平台good

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

2016-04-28  本文已影响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文件我没有写代码也就是主界面了



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

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

上一篇下一篇

猜你喜欢

热点阅读