利用UIScrollView制作一个类似于Appstroe中应用介绍详情里面点击图片放大的引导页,直接上代码
效果图如下
ViewController.h
@interface ViewController : UIViewController<UIScrollViewDelegate>
//滚动视图对象
@property (retain, nonatomic) UIScrollView *scrollView;
//视图中小圆点,对应视图的页码
@property (retain, nonatomic) UIPageControl *pageControl;
//动态数组对象,存储图片
@property (retain, nonatomic) NSMutableArray *images;
@end
ViewController.m
#import "ViewController.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define imgHeight SCREEN_HEIGHT - 160
#define imgWidth SCREEN_WIDTH - 65
- (void)viewDidLoad {
[super viewDidLoad];
//初始化数组,存储滚动视图的图片
self.images = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"text1.png"],[UIImage imageNamed:@"text2.png"],[UIImage imageNamed:@"text3.png"],[UIImage imageNamed:@"text4.png"], nil];
//设置视图的背景颜色
self.view.backgroundColor = [UIColor whiteColor];
[self initScrollView];
}
-(void)initScrollView{
//初始化滚动视图
//注意此处scrollView的宽度,如果不想每次翻页都滚动一个屏幕的宽度,需要自己设置一般是图片宽度加间隙
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 106.5, imgWidth + 19.5, imgHeight)];
//设置按页滚动
self.scrollView.pagingEnabled = YES;
//隐藏滚动条
self.scrollView.showsHorizontalScrollIndicator = NO;
//设置滚动的范围
self.scrollView.contentSize = CGSizeMake(self.images.count * (imgWidth + 19.5), 0);
//设置为NO时可以显示超出scrollView的部分
self.scrollView.clipsToBounds = NO;
//设置代理
self.scrollView.delegate = self;
//设置初始偏移量
self.scrollView.contentOffset = CGPointMake(0, 0);
[self.view addSubview:self.scrollView];
//添加图片
for (int i = 0; i<self.images.count; i++) {
CGFloat x = 32.5 + i * (imgWidth + 19.5);
CGRect frame = CGRectMake(x, 0, imgWidth, imgHeight);
UIImageView *photoV = [[UIImageView alloc] initWithFrame:frame];
photoV.image = [self.images objectAtIndex:i];
[self.scrollView addSubview:photoV];
}
//设置底部页数指示标志
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 20)];
_pageControl.numberOfPages = self.images.count;
_pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
_pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
_pageControl.userInteractionEnabled = YES;
_pageControl.currentPage = 0;
//添加点击事件
[_pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_pageControl];
}
//改变页码的方法实现
- (void)changePage:(id)sender
{
NSLog(@"指示器的当前索引值为:%li",(long)self.pageControl.currentPage);
[self.scrollView setContentOffset:CGPointMake(self.pageControl.currentPage * self.scrollView.frame.size.width, 0) animated:YES];
}
//下面两种协议只需实现一种
#pragma mark-----UIScrollViewDelegate---------
//实现协议UIScrollViewDelegate的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//根据scrolView的左右滑动,对pageCotrol的当前指示器进行切换(设置currentPage)
int pageNum = scrollView.contentOffset.x / self.scrollView.frame.size.width;
//切换改变页码,小圆点
self.pageControl.currentPage = pageNum;
}
//最好采用下面这种方法,上面那种方法只有在滚动停止时调用,当滚动很快时,中间过程就没有显示小圆点的变化,只是显示了最后停止时的位置
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
self.pageControl.currentPage = _scrollView.contentOffset.x / _scrollView.frame.size.width;
}
项目代码:
https://git.oschina.net/iOSlwg/ScrollViewdemo/tree/master/