AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
BOOL isOpen = [ud boolForKey:@"FirstRun"];
if (!isOpen) {
PageViewController *page = [PageViewController new];
self.window.rootViewController = page;
[ud setBool:YES forKey:@"FirstRun"];
[ud synchronize];
return YES;
}
return YES;
}
//
// PageViewController.m
// 开机启动引导页
//
// Created by Qianfeng on 15/10/18.
// Copyright (c) 2015年 张向阳. All rights reserved.
//
#import "PageViewController.h"
#include "ViewController.h"
@interface PageViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@end
@implementation PageViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
[self createScrollView];
}
- (void)createScrollView {
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
for (NSInteger i = 0; i < 4; i++) {
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"32_%ld.jpg", i]];
UIImageView *imgView = [[UIImageView alloc] initWithFrame: CGRectMake(i*_scrollView.bounds.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
imgView.image = img;
if (i==3) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.bounds = CGRectMake(100, 100, 100, 50);
button.center = self.view.center;
button.backgroundColor = [UIColor greenColor];
[button setTitle:@"开始体验" forState:UIControlStateNormal];
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[imgView addSubview:button];
imgView.userInteractionEnabled = YES; // 打开用户交互
}
[_scrollView addSubview:imgView];
}
_scrollView.contentSize = CGSizeMake(4*_scrollView.bounds.size.width, _scrollView.bounds.size.height);
_scrollView.bounces = NO;
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
[self.view addSubview:_scrollView];
[self createPageControl];
}
- (void)createPageControl {
UIPageControl *pageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-30, self.view.bounds.size.width, 30)];
pageControl.numberOfPages=4;
pageControl.tag=301;
[pageControl addTarget:self action:@selector(pageClick:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pageControl];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
UIPageControl *page=(UIPageControl *)[self.view viewWithTag:301];
page.currentPage=scrollView.contentOffset.x/scrollView.bounds.size.width;
}
-(void)pageClick:(UIPageControl *)pageControl{
[_scrollView setContentOffset:CGPointMake(pageControl.currentPage*_scrollView.bounds.size.width, 0) animated:YES];
}
- (void)btnClick:(UIButton *)senter {
ViewController *vc = [ViewController new];
[self presentViewController:vc animated:YES completion:nil];
}
@end