基本功能
//创建滚动视图
UIScrollView * myScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//滚动视图上加载的控件大小(可大于滚动视图)
myScrollView.contentSize = CGSizeMake(320 * (images.count + 2), 568);
//设定滚动视图首次加载的位置(即偏移量)
myScrollView.contentOffset = CGPointMake(320, 0);
//其set方法
[myScrollView setContentOffset:CGPointMake(320 *(pageCount+1), 0) animated:YES];
//设置翻滚及翻页效果
[myScrollView scrollRectToVisible:CGRectMake(320 * 4, 0, 320, 568) animated:NO];
//animated:控制是否显示快速翻页时的动作效果,关闭则直接显示翻页到当前页,开启则会显示中间的翻动过程
//关闭滚动视图回弹效果,当需要让滚动视图自动循环播放时,该选项需关闭
myScrollView.bounces = YES;//默认为开启
//开启分页效果(当将页面拉至一半时会自动滑到其中一页)
myScrollView.pagingEnabled = YES;
//隐藏滚动条
myScrollView.showsHorizontalScrollIndicator = NO;
//将滚动视图加载到主视图上
[self.view addSubview:myScrollView];
循环滚动功能
循环滚动有两个实现方式
实现方法一
- (void)viewDidLoad {
[super viewDidLoad];
myScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//关闭滚动视图回弹效果
//myScrollView.bounces = YES;
//开启分页效果�
myScrollView.pagingEnabled = YES;
myScrollView.showsHorizontalScrollIndicator = NO;//隐藏滚动条
myScrollView.delegate = self;
[self.view addSubview:myScrollView];
UIImageView * firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
firstImageView.image = [UIImage imageNamed:@"d"];
[myScrollView addSubview:firstImageView];
NSArray * images = @[@"a",@"b",@"c",@"d"];
for (int i = 0; i < images.count; i++) {
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320 * i + 320, 0, 320, 568)];
imageView.image = [UIImage imageNamed:images[i]];
[myScrollView addSubview:imageView];
}
UIImageView * lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(320 * images.count + 320, 0, 320, 568)];
lastImageView.image = [UIImage imageNamed:@"a"];
[myScrollView addSubview:lastImageView];
myScrollView.contentSize = CGSizeMake(320 * (images.count + 2), 568);
myScrollView.contentOffset = CGPointMake(320, 0);
pageCount = 0;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(change:) userInfo:nil repeats:YES];
}
-(void)change:(id)sender
{
if (pageCount == 4) {
pageCount = 1;
[myScrollView scrollRectToVisible:CGRectMake(320 , 0, 320, 568) animated:NO];
}else{
pageCount++;
}
[myScrollView setContentOffset:CGPointMake(320 *(pageCount+1), 0) animated:YES];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//第一页和倒数第二页一致,倒数最后一页和第二页一致,当跳到第一页或最后一页时直接跳转,因为页面一样,跳过去并不会有明显的动画,接下来则是正常的页面跳转。
if ((int)scrollView.contentOffset.x % (int)self.view.bounds.size.width == 0) {
if ((int)scrollView.contentOffset.x / (int)self.view.bounds.size.width == 0) {
//视图向前滚动到第一页(即多出来表示最后一页的页面),则自动跳到倒数第二页(表面上的最后一页)。
[myScrollView scrollRectToVisible:CGRectMake(320 * 4, 0, 320, 568) animated:NO];
}else if ((int)scrollView.contentOffset.x / (int)self.view.bounds.size.width == 5){
//视图滚动到最后一页(即表面上的第一页),则自动跳到第二页
[myScrollView scrollRectToVisible:CGRectMake(320 , 0, 320, 568) animated:NO];
}
}
}
实现方法二
此处未实现手动滑动的功能
viewDidLoad
:
#pragma mark 顶部ScrollView
//高度180
topScrollView = [[UIScrollView alloc]init];
[headerView addSubview:topScrollView];
[topScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(headerView.mas_left).with.offset(0);
make.top.equalTo(headerView.mas_top);
make.right.equalTo(headerView.mas_right).with.offset(0);
make.height.mas_equalTo(@(HEIGHT_FOR_TOP_SCROLLVIEW));
}];
topScrollView.pagingEnabled = YES;
topScrollView.showsHorizontalScrollIndicator = NO;
topScrollView.showsVerticalScrollIndicator = NO;
topScrollView.delegate = self;
//4张图,正向循环滚动
NSArray * imageTitleArray = @[@"banner轮播图4",@"banner轮播图1",@"banner轮播图2",@"banner轮播图3",@"banner轮播图4",@"banner轮播图1"];
topScrollView.contentSize = CGSizeMake(WIDTH_FOR_SCREEN * imageTitleArray.count, HEIGHT_FOR_TOP_SCROLLVIEW);
topScrollView.contentOffset = CGPointMake(WIDTH_FOR_SCREEN, 0);
for (int i = 0; i < imageTitleArray.count; i++) {
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*WIDTH_FOR_SCREEN, 0, WIDTH_FOR_SCREEN, HEIGHT_FOR_TOP_SCROLLVIEW)];
imageView.image = [UIImage imageNamed:imageTitleArray[i]];
[topScrollView addSubview:imageView];
}
pageCount = 1;//1代表第二张,即初始张
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(change:) userInfo:nil repeats:YES];
//创建分页指示器
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, HEIGHT_FOR_TOP_SCROLLVIEW - 10,WIDTH_FOR_SCREEN , 15)];
//指定pageControl相关参数
pageControl.numberOfPages = 4;
// pageControl.backgroundColor = [UIColor clearColor];
[pageControl addTarget:self action:@selector(pageControlClick:) forControlEvents:UIControlEventTouchUpInside];//设置分页指示器点击方法
[headerView addSubview:pageControl];
方法
:
//分页指示器可以根据需要设置只显示需要的部分,前后两个画面不设置
- (void)change:(UIButton *)sender
{
[topScrollView setContentOffset:CGPointMake(WIDTH_FOR_SCREEN + topScrollView.contentOffset.x, 0) animated:YES];
//此处可根据需要手动设置分页指示器
/*
int index = fabs(scrollView.contentOffset.x/LHQSCREEN_W);
self.figurePageControl.currentPage = index;
*/
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x == 5 * WIDTH_FOR_SCREEN) {
[scrollView setContentOffset:CGPointMake(WIDTH_FOR_SCREEN, 0) animated:NO];
}
}
-(void)pageControlClick:(UIPageControl *)sender
{
NSLog(@"%ld",sender.currentPage);
//通过offset偏移量来修改当前滚动视图的滚动位置
[topScrollView setContentOffset:CGPointMake(WIDTH_FOR_SCREEN * sender.currentPage, 0) animated:YES];
}
交互
scrollView上加载scrollView,防止触摸事件干扰
使用setExclusiveTouch
方法,可以达到同一界面上多个控件接受事件时的排他性
常用方法备注:
#define HEIGHT_FOR_SCREEN [UIScreen mainScreen].bounds.size.height
#define WIDTH_FOR_SCREEN [UIScreen mainScreen].bounds.size.width
//协议:<UIScrollViewDelegate>