IOS---UIScrollView(无限循环滚动)

#UIScrollView自动无限循环滑动

##1、UIScrollView概念

  • UIScrollView是滚动的view,UIView本身不能滚动,子类UIScrollview拓展了滚动方面的功能。

  • UIScrollView是所有滚动视图的基类。以后的UITableView,UITextView等视图都是继承于该类。

  • 使用场景:显示不下(单张大图);内容太多(图文混排);滚动头条(图片);相册等

  • 无限循环滚动有两个方法,一个是根据图片的数量前后+1,另一个是3个视图显示,当图片数量很多时,用第一种方法需要创建很多个视图,要占用很大内存,这时就需要第二种方法了,下面就介绍一下第二种方法的实现
    ps :用CATransition(转场动画)也可以实现一个视图无限循环切换图片 代码示例:

#import "ViewController.h"

#define Width [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UIScrollViewDelegate>

@property (strong, nonatomic) NSArray *imageData;
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIImageView *leftImageView;
@property (strong, nonatomic) UIImageView *centerImageView;
@property (strong, nonatomic) UIImageView *rightImageView;
@property (assign, nonatomic) NSInteger centerImageIndex;
@property (strong, nonatomic) UIPageControl *pageControl;



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadImageData];
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageControl];
    [self loadImageView];
   
    
}
#pragma mark - 创建视图并定义初始图片
- (void)loadImageView {
    self.leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Width, Height)];
    self.leftImageView.image = [UIImage imageNamed:self.imageData[self.imageData.count-1]];
    
    self.centerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(Width, 0, Width, Height)];
    self.centerImageView.image = [UIImage imageNamed:self.imageData[0]];

    self.rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(Width*2, 0, Width, Height)];
    self.rightImageView.image = [UIImage imageNamed:self.imageData[1]];
    
    [self.scrollView addSubview:self.leftImageView];
    [self.scrollView addSubview:self.centerImageView];
    [self.scrollView addSubview:self.rightImageView];
    self.centerImageIndex = 0;
    [self.scrollView setContentOffset:CGPointMake(Width, 0) animated:NO];
    
}

- (void)loadImageData {
    // 获取文件的本地路径
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"imageData" ofType:@"plist"];
    // 将路径转为对应的数据类型(NSArray)
    self.imageData = [NSArray arrayWithContentsOfFile:plistPath];

}
#pragma mark - 偏移后重新加载图片
- (void)loadImage {
    NSInteger leftImageIndex,rightImageIndex;
    CGPoint offset = [self.scrollView contentOffset];
    if (offset.x > Width) {
        //向右偏移 用%可以到图片末尾自动切换到开始
        self.centerImageIndex = (self.centerImageIndex + 1) % self.imageData.count;
    }
    if (offset.x < Width) {
        //向左偏移
        self.centerImageIndex = (self.centerImageIndex +self.imageData.count - 1) % self.imageData.count;
        
    }
    self.centerImageView.image = [UIImage imageNamed:self.imageData[self.centerImageIndex]];
    leftImageIndex = (self.centerImageIndex +self.imageData.count - 1) % self.imageData.count;
    rightImageIndex = (self.centerImageIndex +1) % self.imageData.count;
    
    self.leftImageView.image = [UIImage imageNamed:self.imageData[leftImageIndex]];
    self.rightImageView.image = [UIImage imageNamed:self.imageData[rightImageIndex]];
}

#pragma mark - 停止滚动时
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self loadImage];
    [self.scrollView setContentOffset:CGPointMake(Width, 0) animated:NO];
    self.pageControl.currentPage = self.centerImageIndex;

}

#pragma mark - 懒加载视图
- (UIPageControl *)pageControl {
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, Height - 30, Width, 2)];
        _pageControl.numberOfPages = self.imageData.count;
        _pageControl.pageIndicatorTintColor = [UIColor orangeColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        
    }
    return _pageControl;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,Width, Height)];
         _scrollView.contentSize = CGSizeMake(Width*3, Height);
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.delegate = self;
    }
    return _scrollView;
}
- (NSArray *)imageData {
    if (!_imageData) {
        _imageData = [NSArray array];
    }
    return _imageData;
}
@end

实现效果:

Demo地址:

https://github.com/fuxinto/studyDemo

转载于:https://my.oschina.net/6603044/blog/738781

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值