UIScrollerView 无限滚动视图

#import "PictureViewController.h"
@interface PictureViewController () <UIScrollViewDelegate>
{
    UIScrollView * _pictureScorll;
    UIImage * _transPictureImage;
    NSMutableArray * _imageViews;
    NSMutableArray * _imageNames;
}

- (void)initPictureVC;
@end

@implementation PictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
//    [self initPictureVC];
    [self setImageFormImageNames];
}


//图片数据

- (void)initData
{
    _imageNames = [[NSMutableArray alloc] initWithObjects:@"1.png",@"2.png",@"3.png",@"4.png",@"5.png",@"6.png",@"7.png",@"8.png",@"9.png",@"10.png",@"11.png", nil];
    _imageViews = [[NSMutableArray alloc] init];
}

- (void)initPictureVC
{

#pragma mark - 添加照片
//滚动视图的设置
    self.automaticallyAdjustsScrollViewInsets = NO; //关闭导航栏下面留白
    _pictureScorll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
    _pictureScorll.backgroundColor = [UIColor blackColor];
    [_pictureScorll setContentSize:CGSizeMake(CGRectGetWidth(_pictureScorll.frame)*3, CGRectGetHeight(_pictureScorll.frame)-140)]; //配置最大滚动范围
    [_pictureScorll setContentOffset:CGPointMake(CGRectGetWidth(_pictureScorll.frame), 0)];//设置一开始的偏移量到第二个视图的位置
    _pictureScorll.showsHorizontalScrollIndicator = NO;//配置水平指示器
    _pictureScorll.showsVerticalScrollIndicator = NO;//配置垂直指示器
    _pictureScorll.pagingEnabled = YES;  //配置按页滚动
    _pictureScorll.bounces = YES; //配置边界回弹效果
    _pictureScorll.delegate = self; //设置代理
    [self.view addSubview:_pictureScorll];
    //3个图片视图的操作
    for (int i = 0; i < 3; i ++) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * CGRectGetWidth(_pictureScorll.frame), 0, CGRectGetWidth(_pictureScorll.frame), CGRectGetHeight(_pictureScorll.frame))];
        [_pictureScorll addSubview:imageView]; //将图片视图加入滚动视图
        [_imageViews addObject:imageView];//将图片视图加入到图片视图数组中,一共有3个图片视图
        [imageView release];
    }
    
加入照片到滚动视图
//    for (int i = 0; i < 11; i ++) {
//        UIImageView * pictureImage = [[UIImageView alloc]initWithFrame:CGRectMake(i * CGRectGetWidth(_pictureScorll.frame), 0, CGRectGetWidth(_pictureScorll.frame), CGRectGetHeight(_pictureScorll.frame))];
//        [pictureImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i + 1]]];
//        //        pictureImage.contentMode = UIViewContentModeScaleAspectFit;
//        pictureImage.clipsToBounds = YES;
//        [_pictureScorll addSubview:pictureImage];
//        
//        [pictureImage release];
//    }
设置页面控制器
//    UIPageControl * pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 250, 30)];
//    pageControl.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds)+210);
//    pageControl.numberOfPages = 11; //配置页面个数
//    pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:11/255.0 green:179/255.0 blue:252/255.0 alpha:0.85];
//    pageControl.pageIndicatorTintColor = [UIColor grayColor];
//    [pageControl addTarget:self action:@selector(pressedPageControl:) forControlEvents:UIControlEventTouchUpInside];
//    pageControl.tag = 88;
//    [self.view addSubview:pageControl];
//    [_pictureScorll release];
}

//给3个图片视图设置图片
- (void)setImageFormImageNames
{
//    int i = 0;
//    for (UIImageView * imageView in _imageViews) {
//        [imageView setImage:[UIImage imageNamed:_imageNames[i]]];
//        i ++;
//    }
    for (int i = 0; i < 3; i ++) {
        
        [_imageViews[i] setImage:[UIImage imageNamed:_imageNames[i]]];
    }
}

//滚动视图滚动时
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x >= CGRectGetWidth(_pictureScorll.frame) * 2) {//偏移量在初始化时就有1倍的宽了,再次移动就是两倍
        NSString * firstImageName = [[NSString alloc] initWithString:_imageNames.firstObject];//取出排在第一的图片名
        [_imageNames removeObjectAtIndex:0];//再把第一个图片名移除
        [_imageNames addObject:firstImageName];//把取出的名字又加在图片名数组的后面
        [firstImageName release];
    }else if (scrollView.contentOffset.x <= 0) {//偏移量为负值,即向左移动
        NSString * lastImageName = [[NSString alloc] initWithString:_imageNames.lastObject];//取出最后一个图片数组的名字
        [_imageNames removeLastObject];//移除取出的名字
        [_imageNames insertObject:lastImageName atIndex:0];//把取出的名字加在图片名字数组的第一个位子
        [lastImageName release];
    }else {
        return;
    }
    [self setImageFormImageNames];
    [_pictureScorll setContentOffset:CGPointMake(CGRectGetWidth(_pictureScorll.frame), 0)];

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值