图片轮播

#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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,393评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,790评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,391评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,703评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,613评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,003评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,507评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,158评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,300评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,256评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,274评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,984评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,569评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,662评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,899评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,268评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,840评论 2 339

推荐阅读更多精彩内容