您的位置:首页 > 移动开发 > IOS开发

iOS 引导页

2016-07-04 00:00 375 查看
摘要: 目前多数app在启动时会有引导页,今天给大家介绍一种比较直观,能够快速实现的引导页实现方法,最终效果就是有一个全屏的引导页(UIScrollView),页面底部有UIPageControl。

/**

承载引导页的控制器,主页面HomePageViewController.h

*/

#import "UserGuideViewController.h"

#import "HomePageViewController.h"

#define K_MainScreen_width [UIScreen mainScreen].bounds.size.width

#define K_MainScreen_height [UIScreen mainScreen].bounds.size.height

@interface UserGuideViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong)UIPageControl *pageControl;

@end

@implementation UserGuideViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor redColor];

[self initGuide];

}

#pragma mark - 初始化引导页;

- (void)initGuide{

UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,K_MainScreen_width , K_MainScreen_height)];

// 设置滑动的范围,3 倍的屏幕,Y为0 横向滑动

[scrollView setContentSize:CGSizeMake(3 * K_MainScreen_width, 0)];

scrollView.delegate = self;

// 整页滑动

scrollView.pagingEnabled = YES;

// 禁止回弹,防止漏出根视图

scrollView.bounces = NO;

[self.view addSubview:scrollView];

// pageControll

_pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(K_MainScreen_width/3, K_MainScreen_height/3 * 2 + 50, K_MainScreen_width/3, 10)];

_pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];

_pageControl.pageIndicatorTintColor = [UIColor greenColor];

_pageControl.numberOfPages = 3;

_pageControl.currentPage = 0;

[self.view addSubview:_pageControl];

UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, K_MainScreen_width, K_MainScreen_height)];

imageView1.image = [UIImage imageNamed:@"3196253_091326185000_2.jpg"];

[scrollView addSubview:imageView1];

UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(K_MainScreen_width, 0, K_MainScreen_width, K_MainScreen_height)];

imageView2.image = [UIImage imageNamed:@"20140531221642_uyhvL.jpg"];

[scrollView addSubview:imageView2];

UIImageView *imageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(2 * K_MainScreen_width, 0, K_MainScreen_width, K_MainScreen_height)];

imageView3.image = [UIImage imageNamed:@"3167975_143302780126_2.jpg"];

[scrollView addSubview:imageView3];

// 开始按钮

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(K_MainScreen_width/3, K_MainScreen_height/3 * 2, K_MainScreen_width/3, 40);

[button setTitle:@"开始进入" forState:UIControlStateNormal];

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

imageView3.userInteractionEnabled = YES;

[imageView3 addSubview:button];

}

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

_pageControl.currentPage =scrollView.contentOffset.x/ K_MainScreen_width;

}

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

HomePageViewController *homePage = [[HomePageViewController alloc]init];

UINavigationController *NaVC = [[UINavigationController alloc]initWithRootViewController:homePage];

[UIApplication sharedApplication].keyWindow.rootViewController = NaVC;

}

/**

AppDelegate.h中判断是否第一次启动。

*/

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

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

self.window.backgroundColor = [UIColor whiteColor];

[self.window makeKeyAndVisible];

// 判断是否是第一次启动应用

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

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

NSLog(@"这是第一次启动,添加引导页");

UserGuideViewController *userGuideVC = [[UserGuideViewController alloc]init];

self.window.rootViewController = userGuideVC;

}else {

HomePageViewController *homePage = [[HomePageViewController alloc]init];

UINavigationController *NaVC = [[UINavigationController alloc]initWithRootViewController:homePage];

self.window.rootViewController = NaVC;

}

return YES;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: