#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