利用三块空间无限循环滚动视图代码和效果

这篇文章主要是方便读者使用,原理在上一篇文章中.
http://www.jianshu.com/p/a6ad471aa86f

AppDelegate.h

#import "AppDelegate.h"
#import "ZCSViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    ZCSViewController *vc = [[ZCSViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    nav.navigationBar.translucent = NO;
    nav.navigationBar.hidden = YES;
    self.window.rootViewController = nav;
    
    return YES;
}
@end

imageInfo.plist的创建需要自己来完成.

ZCSViewController.m

#import "ZCSViewController.h"
#define SCREEN_WIDTH 375
#define SCREEN_HEIGHT 667
#define IMAGEVIEW_COUNT 3

@interface ZCSViewController ()
//滚动视图
@property(nonatomic,retain)UIScrollView *scrollView;
//左图片
@property(nonatomic,retain)UIImageView *leftImageView;
//中间图片
@property(nonatomic,retain)UIImageView *centerImageView;
//右边图片
@property(nonatomic,retain)UIImageView *rightImageView;
//页面控制器
@property(nonatomic,retain)UIPageControl *pageControl;
//图片的标题
@property(nonatomic,retain)UILabel *label;
@property(nonatomic,retain)NSMutableDictionary *imageData;//图片数据
@property(nonatomic,assign)NSInteger currentImageIndex;//当前图片下标
@property(nonatomic,assign)NSInteger imageCount;//图片总数


@end

@implementation ZCSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void)loadView
{
    [super loadView];
#pragma mark 加载图片数据
    //读取程序包路径中的资源文件
    NSString *path = [[NSBundle mainBundle] pathForResource:@"imageInfo" ofType:@"plist"];
    self.imageData = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    self.imageCount = self.imageData.count;
#pragma mark 添加scrollView控件
    self.scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:_scrollView];
    //设置代理
    self.scrollView.delegate = self;
    //设置contentSize
    self.scrollView.contentSize = CGSizeMake(IMAGEVIEW_COUNT * SCREEN_WIDTH, SCREEN_HEIGHT) ;
    //设置当前显示的位置为中间图片
    self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    //设置分页
    self.scrollView.pagingEnabled = YES;
    //去掉滚动条
    self.scrollView.showsHorizontalScrollIndicator = NO;
#pragma mark 添加图片三个控件
    self.leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.scrollView addSubview:_leftImageView];
    self.centerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];

    [self.scrollView addSubview:_centerImageView];
    self.rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(2 * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];

    [self.scrollView addSubview:_rightImageView];
#pragma mark 添加分页控件
    self.pageControl = [[UIPageControl alloc]init];
    //注意此方法可以根据页数返回UIPageControl合适的大小
    CGSize size = [self.pageControl sizeForNumberOfPages:self.imageCount];
    self.pageControl.bounds = CGRectMake(0, 0, size.width, size.height);
    self.pageControl.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT - 100);
    //设置总页数
    self.pageControl.numberOfPages = self.imageCount;
    
    [self.view addSubview:_pageControl];
#pragma mark 添加信息描述控件
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 30)];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.textColor = [UIColor colorWithRed:0 green:150/255.0 blue:1 alpha:1];
    
    [self.view addSubview:_label];
    
    
#pragma mark 设置默认显示图片
    //加载默认图片
    self.leftImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.JPG",_imageCount - 1]];
    self.centerImageView.image = [UIImage imageNamed:@"0.JPG"];
    self.rightImageView.image = [UIImage imageNamed:@"1.JPG"];
    self.currentImageIndex = 0;
    //设置当前页
    self.pageControl.currentPage = self.currentImageIndex;
}




#pragma mark 滚动停止事件
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //重新加载图片
    [self reloadImage];
    //移动到中间
    self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    //设置分页
    self.pageControl.currentPage = self.currentImageIndex;
    //设置描述
    NSString *imageName = [NSString stringWithFormat:@"%ld.JPG",_currentImageIndex];
    self.label.text = self.imageData[imageName];
}

#pragma mark 重新加载图片
-(void)reloadImage{
    NSInteger leftImageIndex,rightImageIndex;
    CGPoint offset = [self.scrollView contentOffset];
    if (offset.x > SCREEN_WIDTH) { //向右滑动
        self.currentImageIndex = (self.currentImageIndex + 1) % self.imageCount;
    }else if(offset.x < SCREEN_WIDTH){ //向左滑动
        self.currentImageIndex = (self.currentImageIndex + self.imageCount - 1) % self.imageCount;
    }

    self.centerImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.JPG",self.currentImageIndex]];
    
    //重新设置左右图片
    leftImageIndex = (self.currentImageIndex + self.imageCount - 1) % self.imageCount;
    rightImageIndex = (self.currentImageIndex + 1) % self.imageCount;
    self.leftImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.JPG",leftImageIndex]];
    self.rightImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.JPG",rightImageIndex]];
}

@end

效果图:

利用三块空间无限循环滚动视图<UIScrollView>代码和效果_第1张图片
Simulator Screen Shot 2016年4月12日 上午10.52.36.png
利用三块空间无限循环滚动视图<UIScrollView>代码和效果_第2张图片
Simulator Screen Shot 2016年4月12日 上午10.52.55.png
利用三块空间无限循环滚动视图<UIScrollView>代码和效果_第3张图片
Simulator Screen Shot 2016年4月12日 上午10.52.57.png
利用三块空间无限循环滚动视图<UIScrollView>代码和效果_第4张图片
Simulator Screen Shot 2016年4月12日 上午10.52.59.png
利用三块空间无限循环滚动视图<UIScrollView>代码和效果_第5张图片
Snip20160412_5.png

你可能感兴趣的:(利用三块空间无限循环滚动视图代码和效果)