圖片輪播

#import "ViewController.h"

#import "MineView.h"

@interface ViewController ()

@end

@implementation?

ViewController{?

?? NSArray*_imageList;

}

- (void)viewDidLoad {

[super viewDidLoad];

[self loadData];

[self setupUI];

}

- (void)loadData {

NSMutableArray *arrayM = [NSMutableArray array];

for (NSInteger i = 0; i < 5; i++) {

NSString *imageName = [NSString stringWithFormat:@"Home_Scroll_%02zd.jpg",i+1];

NSString *path = [[NSBundle mainBundle]pathForResource:imageName ofType:nil];

UIImage *image = [UIImage imageWithContentsOfFile:path];

//相對于UIImage imageNamed: 沒有緩存

[arrayM addObject:image];

}

_imageList = arrayM.copy;

}

- (void)setupUI{

MineView *mineView = [[MineView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 120)];

self.view.backgroundColor = [UIColor whiteColor];

mineView.imageList = _imageList;

self.tableView.tableHeaderView = mineView;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end



MineView:


#import@interface MineView : UIView

@property (nonatomic,strong) NSArray *imageList;

@end





#import "MineView.h"

#import "Masonry.h"

#import "MineFlowLayout.h"

#import "MineCollectionViewCell.h"

static NSString *cellID = @"cellID";

@interface MineView ()

@property (nonatomic,weak) UICollectionView *collectionView;

@property (nonatomic,weak) UIPageControl *pageControl;

@property (nonatomic,strong) NSTimer *timer;

@end

@implementation MineView

- (instancetype)initWithFrame:(CGRect)frame {

if (self = [super initWithFrame:frame]) {

[self setupUI];

}

return self;

}

//view從父控件移除的時候 會調用此方法

- (void)removeFromSuperview {

[super removeFromSuperview];

//停止定時器

[self.timer invalidate];

}

- (void)setImageList:(NSArray *)imageList {

_imageList = imageList;

//立即 布局子控件 因為 數據有了cell 還沒有

//先調用 此方法 對子控件進行強制布局

[self layoutIfNeeded];

//拿到數據后就滾動到第五個 是為了避免 剛開始向左滾動 無法滾動的問題

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_imageList.count inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

self.pageControl.numberOfPages = imageList.count;

}

- (void)setupUI{

MineFlowLayout *flowLayout = [[MineFlowLayout alloc]init];

UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];

self.collectionView = collectionView;

collectionView.dataSource = self;

collectionView.delegate = self;

[collectionView registerClass:[MineCollectionViewCell class] forCellWithReuseIdentifier:cellID];

collectionView.bounces = NO;

collectionView.showsHorizontalScrollIndicator = NO;

collectionView.pagingEnabled = YES;

[self addSubview:collectionView];

[collectionView mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.left.right.offset(0);

make.bottom.offset(-5);

}];

UIPageControl *pageControl = [[UIPageControl alloc]init];

self.pageControl = pageControl;

pageControl.pageIndicatorTintColor = [UIColor colorWithWhite:0.8 alpha:1];

pageControl.currentPageIndicatorTintColor = [UIColor colorWithWhite:0.2 alpha:1];

//? ? pageControl.numberOfPages = 5;

//拿到數據后? 對numberOfPage賦值

[self addSubview:pageControl];

[pageControl mas_makeConstraints:^(MASConstraintMaker *make) {

make.centerX.offset(0);

make.bottom.offset(15);

}];

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(playPicture) userInfo:nil repeats:YES];

self.timer = timer;

//scheduledTimerWithTimeInterval 以默認模式添加到運行循環里

//只是創建 一個控制器,不把它添加到運行循環

//? ? NSTimer *timer = NSTimer timerWithTimeInterval:<#(NSTimeInterval)#> target:<#(nonnull id)#> selector:<#(nonnull SEL)#> userInfo:<#(nullable id)#> repeats:<#(BOOL)#>

//默認模式不能同時處理界面的變化和定時器

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

//拖拽的時候 定時器暫停

self.timer.fireDate = [NSDate distantFuture];

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

//重新開發定時器

self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];

}

//加入 自動播放后 會有bug? 動畫播放時采用動畫的效果 不會調用scrollViewDidEndDecelerating

- (void)playPicture {

CGPoint offset = self.collectionView.contentOffset;

offset.x += self.collectionView.frame.size.width;

//修改后的偏移量 重新賦值給collectionView

//? ? self.collectionView.contentOffset = offset;

[self.collectionView setContentOffset:offset animated:YES];

//0.25s

}

//當停止動畫滾動 會調用此方法

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {

//頁碼就是第幾個cell? 先拿到scrollView滾動了幾個屏幕的寬度

NSInteger page = scrollView.contentOffset.x / scrollView.bounds.size.width;

//拿到collectionViewcell的個數

UICollectionView *collectionView = (UICollectionView *)scrollView;

NSInteger cellCount = [collectionView numberOfItemsInSection:0];

//當scrollView停止滾動的時候,判斷是否是最后一個cell,如果是跳轉到? _imageList.count - 1

if (page == cellCount - 1) {

//說明滾動到了最后cell

[collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_imageList.count - 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

}

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

return _imageList.count * 2;

//放兩個 _imageList

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

MineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];

//? ? cell.image = _imageList[indexPath.item];

cell.image = _imageList[indexPath.item % _imageList.count];

//有五張圖片 item取得結果是0-4? 取模運算 取到的結果 也是0-4

return cell;

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

NSInteger page = round(scrollView.contentOffset.x / scrollView.bounds.size.width);

//? ? self.pageControl.currentPage = round(scrollView.contentOffset.x / scrollView.bounds.size.width);

self.pageControl.currentPage = page % _imageList.count;

}

/*

scrollView 停止的時候 可能會調用三個方法 didEndDraging? didEndDecelerating? endScrolAnimated

*/

// a b c d e? a b c d e

//監聽滾動停止的 狀態? 手指停止拖動 立馬停止滾動

//左右 滾動的時候,一組數據滾完后,會有卡頓現象? 滾動停止才會調用這個方法

//而且如果剛開始 就往左滾動 無法滾動? 解決方案:一上來就運行在第五個上

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

//頁碼就是第幾個cell? 先拿到scrollView滾動了幾個屏幕的寬度

NSInteger page = scrollView.contentOffset.x / scrollView.bounds.size.width;

//拿到collectionViewcell的個數

UICollectionView *collectionView = (UICollectionView *)scrollView;

NSInteger cellCount = [collectionView numberOfItemsInSection:0];

//當scrollView停止滾動的時候,判斷是否是最后一個cell,如果是跳轉到? _imageList.count - 1

if (page == cellCount - 1) {

//說明滾動到了最后cell

[collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_imageList.count - 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

}

if (page == 0) {

[collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_imageList.count inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

}

}

@end



MineFlowLayout

#import "MineFlowLayout.h"

@implementation MineFlowLayout

- (void)prepareLayout {

[super prepareLayout];

self.itemSize = self.collectionView.bounds.size;

self.scrollDirection = UICollectionViewScrollDirectionHorizontal;

self.minimumLineSpacing = 0;

self.minimumInteritemSpacing = 0;

}

@end



MineCollectionViewCell

#import@interface MineCollectionViewCell : UICollectionViewCell

@property (nonatomic,strong) UIImage *image;

@end


#import "MineCollectionViewCell.h"

#import "Masonry.h"

@interface MineCollectionViewCell ()

@property (nonatomic,weak) UIImageView *pictureView;

@end

@implementation MineCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {

if (self = [super initWithFrame:frame]) {

[self setupUI];

}

return self;

}

- (void)setupUI{

UIImageView *imageView = [[UIImageView alloc]init];

[self.contentView addSubview:imageView];

self.pictureView = imageView;

[imageView mas_makeConstraints:^(MASConstraintMaker *make) {

make.edges.offset(0);

}];

}

- (void)setImage:(UIImage *)image {

_image = image;

self.pictureView.image = image;

}

@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容