IOS---UIScrollView(無限循環滾動)

#UIScrollView自動無限循環滑動git

##一、UIScrollView概念github

  • UIScrollView是滾動的view,UIView自己不能滾動,子類UIScrollview拓展了滾動方面的功能。動畫

  • UIScrollView是全部滾動視圖的基類。之後的UITableView,UITextView等視圖都是繼承於該類。atom

  • 使用場景:顯示不下(單張大圖);內容太多(圖文混排);滾動頭條(圖片);相冊等code

  • 無限循環滾動有兩個方法,一個是根據圖片的數量先後+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/studyDemoget

相關文章
相關標籤/搜索