#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];
//相對(duì)于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從父控件移除的時(shí)候 會(huì)調(diào)用此方法
- (void)removeFromSuperview {
[super removeFromSuperview];
//停止定時(shí)器
[self.timer invalidate];
}
- (void)setImageList:(NSArray *)imageList {
_imageList = imageList;
//立即 布局子控件 因?yàn)?數(shù)據(jù)有了cell 還沒有
//先調(diào)用 此方法 對(duì)子控件進(jìn)行強(qiáng)制布局
[self layoutIfNeeded];
//拿到數(shù)據(jù)后就滾動(dòng)到第五個(gè) 是為了避免 剛開始向左滾動(dòng) 無(wú)法滾動(dòng)的問(wèn)題
[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;
//拿到數(shù)據(jù)后? 對(duì)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 以默認(rèn)模式添加到運(yùn)行循環(huán)里
//只是創(chuàng)建 一個(gè)控制器,不把它添加到運(yùn)行循環(huán)
//? ? NSTimer *timer = NSTimer timerWithTimeInterval:<#(NSTimeInterval)#> target:<#(nonnull id)#> selector:<#(nonnull SEL)#> userInfo:<#(nullable id)#> repeats:<#(BOOL)#>
//默認(rèn)模式不能同時(shí)處理界面的變化和定時(shí)器
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
//拖拽的時(shí)候 定時(shí)器暫停
self.timer.fireDate = [NSDate distantFuture];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//重新開發(fā)定時(shí)器
self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
}
//加入 自動(dòng)播放后 會(huì)有bug? 動(dòng)畫播放時(shí)采用動(dòng)畫的效果 不會(huì)調(diào)用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
}
//當(dāng)停止動(dòng)畫滾動(dòng) 會(huì)調(diào)用此方法
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
//頁(yè)碼就是第幾個(gè)cell? 先拿到scrollView滾動(dòng)了幾個(gè)屏幕的寬度
NSInteger page = scrollView.contentOffset.x / scrollView.bounds.size.width;
//拿到collectionViewcell的個(gè)數(shù)
UICollectionView *collectionView = (UICollectionView *)scrollView;
NSInteger cellCount = [collectionView numberOfItemsInSection:0];
//當(dāng)scrollView停止?jié)L動(dòng)的時(shí)候,判斷是否是最后一個(gè)cell,如果是跳轉(zhuǎn)到? _imageList.count - 1
if (page == cellCount - 1) {
//說(shuō)明滾動(dòng)到了最后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;
//放兩個(gè) _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取得結(jié)果是0-4? 取模運(yùn)算 取到的結(jié)果 也是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 停止的時(shí)候 可能會(huì)調(diào)用三個(gè)方法 didEndDraging? didEndDecelerating? endScrolAnimated
*/
// a b c d e? a b c d e
//監(jiān)聽滾動(dòng)停止的 狀態(tài)? 手指停止拖動(dòng) 立馬停止?jié)L動(dòng)
//左右 滾動(dòng)的時(shí)候,一組數(shù)據(jù)滾完后,會(huì)有卡頓現(xiàn)象? 滾動(dòng)停止才會(huì)調(diào)用這個(gè)方法
//而且如果剛開始 就往左滾動(dòng) 無(wú)法滾動(dòng)? 解決方案:一上來(lái)就運(yùn)行在第五個(gè)上
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//頁(yè)碼就是第幾個(gè)cell? 先拿到scrollView滾動(dòng)了幾個(gè)屏幕的寬度
NSInteger page = scrollView.contentOffset.x / scrollView.bounds.size.width;
//拿到collectionViewcell的個(gè)數(shù)
UICollectionView *collectionView = (UICollectionView *)scrollView;
NSInteger cellCount = [collectionView numberOfItemsInSection:0];
//當(dāng)scrollView停止?jié)L動(dòng)的時(shí)候,判斷是否是最后一個(gè)cell,如果是跳轉(zhuǎn)到? _imageList.count - 1
if (page == cellCount - 1) {
//說(shuō)明滾動(dòng)到了最后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