创建一个 UIScrollView 对象,添加 UIScrollViewDelegate 代理
加入引导图片
设置 scrollView 内容大小(contentSize),开启分页(scrollView.pagingEnable = YES)
创建 pageControl,设置分页数(numberOfPages)以及当前页(currentPage)
添加按钮跳转和代理方法
//
// NiuViewController.m
// UIScrollView创建新手引导界面
//
// Created by ilaoniu on 15-7-9.
// Copyright (c) 2015年 ilaoniu. All rights reserved.
//
#import "NiuViewController.h"
@interface NiuViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIPageControl *pageControl;
@end
@implementation NiuViewController
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect ScreenRect = [[UIScreen mainScreen]bounds];
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenRect.size.width, ScreenRect.size.height)];
scrollView.delegate = self;
[self.view addSubview:scrollView];
for (int i=0; i<4; i++) {
UIImage *backImage = [UIImage imageNamed:@"guide_bg"];
UIImageView *backImageView = [[UIImageView alloc]initWithImage:backImage];
backImageView.frame = CGRectMake(320*i, 0, 320, 480);
[scrollView addSubview:backImageView];
NSString *string = [NSString stringWithFormat:@"guideView_%i",i+1];
UIImage *forImage = [UIImage imageNamed:string];
UIImageView *forImageView = [[UIImageView alloc]initWithImage:forImage];
forImageView.frame = CGRectMake(320*i, 0, 320, 480);
[scrollView addSubview:forImageView];
}
scrollView.contentSize = CGSizeMake(320*4, 0);
scrollView.pagingEnabled = YES;
self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(100, 450, 120, 0)];
self.pageControl.numberOfPages = 4;
self.pageControl.currentPage = 0;
[self.view addSubview:self.pageControl];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100+320*3, 420, 120, 40);
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
}
-(void)buttonClicked:(UIButton *)button{
NSLog(@"新手引导完成,进入应用了!");
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int gap = scrollView.contentOffset.x/320;
self.pageControl.currentPage = gap;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end