个人觉得 collection view 做轮播是最方便的,设置下flowlayout 其他不会有很大的变动,没有什么逻辑的代码

let's begin……

创建自定义的view

.h 声明文件

@interface CollectionViewShuffling : UIView

@property (nonatomic,strong)NSArray *array;

@end

.m 实现文件

@interface CollectionViewShuffling ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *collectionArray; @end @implementation CollectionViewShuffling
@synthesize array = _array; -(instancetype)initWithFrame:(CGRect)frame{ if (self == [super initWithFrame:frame]) {
}
return self;
}
/**
这个是横向滚动轮播的重点研究对象
*/
-( UICollectionViewFlowLayout *)creatFlowLayout{
// 创建UICollectionViewFlowLayout约束对象
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
// 设置item的Size大小
flowLayout.itemSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
// 设置uicollection 的 横向滑动
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flowLayout.minimumLineSpacing = ;
return flowLayout;
} - (UICollectionView *)collectionView
{
if (!_collectionView) {
UICollectionViewFlowLayout *flowLayout = [self creatFlowLayout];
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(, , self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];
[self addSubview:_collectionView];
// 设置代理
_collectionView.delegate = self;
_collectionView.dataSource = self;
// _collectionView.showsHorizontalScrollIndicator = NO;// 设置不展示滑动条
_collectionView.pagingEnabled = YES; // 设置整页滑动
// 设置当前collectionView 到哪个位置(indexPath row 0 section 取中间(50个)) [_collectionView registerNib:[UINib nibWithNibName:@"ShufflingItem" bundle:nil] forCellWithReuseIdentifier:@"ShufflingItem"];
}
return _collectionView;
} -(void)setArray:(NSArray *)array{
NSAssert(array.count != , @"传入的滚动数组是 空的");
_array = array;
[self prepareData];
[self prepareUI];
} -(void)prepareUI{ [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem: inSection:] atScrollPosition:UICollectionViewScrollPositionLeft animated:false];
[self.collectionView reloadData];
} - (void)prepareData{ self.collectionArray = [NSMutableArray new];
// 首位 添加数组最后的元素
[self.collectionArray addObject:_array.lastObject];
// 添加数组元素
[self.collectionArray addObjectsFromArray:_array];
// 末尾 补充第一个元素
[self.collectionArray addObject:_array.firstObject];
}
/*
collection view delegate
*/
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return ;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.collectionArray.count;
} - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ShufflingItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShufflingItem" forIndexPath:indexPath];
if (!item) {
item = [[ShufflingItem alloc] init];
}
item.imageView.backgroundColor = self.collectionArray[indexPath.row];
return item;
} -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ if (scrollView == self.collectionView) { NSLog(@"scroll content %@",NSStringFromCGPoint(scrollView.contentOffset)); //检测移动的位移
if (scrollView.contentOffset.x == (self.collectionArray.count-)*(self.frame.size.width) ) { [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem: inSection:] atScrollPosition:UICollectionViewScrollPositionLeft animated:false]; }else if (scrollView.contentOffset.x == ){ [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:(self.collectionArray.count-) inSection:] atScrollPosition:UICollectionViewScrollPositionLeft animated:false];
}else{ // 正常滚动
}
}
}

最简单的collection 轮播,实现啦……

总结下,他的实现为什么如此简单

  1. collection view 有个flow layout 设置这个属性就可以让他横向滚动,竖向滚动
  2. 还有一个重点 声明下 collection view 使用 item cell 的时候,是必须注册的

调用方法::::

-(void)prepareCollectShuffling{

    CollectionViewShuffling *collectShuffling = [[CollectionViewShuffling alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width -, )];
[self.view addSubview:collectShuffling];;
collectShuffling.array = self.arr;
}

这个写完,距离成功又进了一步,继续………………

01.轮播图之三 : collectionView 轮播的更多相关文章

  1. swiper轮播问题之一:轮播图内容为动态数据生成时轮播图无法自动轮播

    本人在用H5做移动端项目中使用Swiper遇到的两个问题,因此加深了对Swiper的掌握,分享出来对刚开始接触Swiper的童鞋们或多或少会有帮助.        首先,new Swiper的初始化最 ...

  2. vue-music 使用better-scroll遇到轮播图不能自动轮播

    根据vue-music视频中slider组建的使用,当安装新版本的better-scroll,轮播组件,不能正常轮播 这是因为,better-scroll发布新版本之后,参数设置发生改变 这是旧版本: ...

  3. 焦点轮播图(tab轮播)

    主要有两部分:1.列表导航(小图片) 2.展示区(大图片) 页面布局: HTML部分:    <div class="s_conC">                  ...

  4. swiper手滑动轮播图后自动轮播失效解决办法

    设置autoplay:true之后,再设置 autoplay:{disableOnInteraction: false} --------------------------------------- ...

  5. JavaScript--缓动动画+轮播图

    上效果: 实现步骤: 最重要的是运动公式!!! <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  6. Jquery 轮播图简易框架

    =====================基本结构===================== <div class="carousel" style="width: ...

  7. 使用jq深入研究轮播图特性

    网站轮播图 太耳熟的词了  基本上做pc端的 主页绝壁会来一个轮播图的特效 轮播图他一个页面页面的切换,其实的原理是通过css的定位 ,定位到一起,第一张首先显示,其余默认隐藏. 今天我实现的这个轮播 ...

  8. iOS回顾笔记(05) -- 手把手教你封装一个广告轮播图框架

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  9. JQ万能轮播图

    lunbotu.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8">  ...

随机推荐

  1. 多任务3(协程)--yield完成多任务交替执行

    协程是并发,单线程,一次执行一个 来回切换 代码: import time def task_1(): while True: print("-----1-----") time. ...

  2. docker(一) -- docker安装、容器加速、下载、备份

    一.docker的 容器是从镜像中创建出来的虚拟实例 容器用来运行实例,是读写层 镜像用来安装程序,是只读层 1. docker的安装和基本操作 安装命令 yum -y update yum inst ...

  3. ElementUI 之 DatePicker 日期限制范围 disabledDate

    需求: 时间选择器,只能选择 2000 年 - 至今的年份. <el-date-picker v-model="year" type="year" :pi ...

  4. GridView修改含有DropDownList控件列的宽度

    GridView进入Edit模式,编辑列动态绑定DropDown List方便客户选择,但当里面的Item过长,不免令界面不美观 正确做法: <asp:TemplateField HeaderT ...

  5. [codevs]线段树练习5

    http://codevs.cn/problem/4927/ #include <iostream> #include <cstdio> #include <algori ...

  6. NetworkX系列教程(1)-创建graph

    小书匠Graph图论 研究中经常涉及到图论的相关知识,而且常常面对某些术语时,根本不知道在说什么.前不久接触了NetworkX这个graph处理工具,发现这个工具已经解决绝大部分的图论问题(也许只是我 ...

  7. Vue的学习--遇到的一些问题和解决方法(二)

    1.关于图片路径问题 1.关于图片路径问题 在.vue的html中可以直接使用相对路径,但是从浏览器后台可以看出,最后路径是自行做了替换的.如果需要在js文件中使用,则需要自己使用require进行替 ...

  8. docker搭建hadoop HA出错问题总结记录。

    错误1: ssh连接云主机: ssh root@39.106.xx.xx 报错:THE AUTHENTICITY OF HOST XX CAN’T BE ESTABLISHED 解决办法: ssh - ...

  9. 深入了解JVM虚拟机8:Java的编译期优化与运行期优化

    java编译期优化 java语言的编译期其实是一段不确定的操作过程,因为它可以分为三类编译过程:1.前端编译:把.java文件转变为.class文件2.后端编译:把字节码转变为机器码3.静态提前编译: ...

  10. scanf和fgets比较

    scanf 长度限制 #include<stdio.h> int main() { char food[5]; printf("Enter food"); scanf( ...