怎样在collectionview的头部头部添加轮播图,要求能随collectionview的头部滚动

怎样在collectionView头部添加轮播图,要求能随collectionView滚动_百度知道
怎样在collectionView头部添加轮播图,要求能随collectionView滚动
n个方块cell即你的dateArray,其余根据tableView一样设置height和数据.count在一个collectionView中基本UI设计应该是一个section
其他类似问题
为您推荐:
轮播的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁iOS开发UI篇—无限轮播(循环利用) - 文顶顶 - 博客园
最怕你一生碌碌无为 还安慰自己平凡可贵
iOS开发UI篇&无限轮播(循环利用)
一、无限轮播&
1.简单说明
  在开发中常需要对广告或者是一些图片进行自动的轮播,也就是所谓的无限滚动。
  在开发的时候,我们通常的做法是使用一个UIScrollView,在UIScrollView上面添加多个imageView,然后设置imageView的图片,和scrollView的滚动范围。
  以前的做法:
  一般而言,轮播的广告或者是图片数量都不会太多(3~5张)。所以,并不会太多的去考虑性能问题。但是如果图片过多(比如有16张图片,就需要创建16个imageView),那么就不得不考虑性能问题了。
  更甚,如果深入做一个图片浏览的小程序,那么可能会处理成百上千张图片,这会造成极大的内存浪费且性能低下。
  图片数量众多:
当用户在查看第一张图片的时候,后面的7张创建的时间太早,且用户可能根本就没机会看见(看完前面几张就没有兴趣再看后面的内容 了)。
优化思路:只有在需要用到的时候,再创建,创建的imageView进行村循环利用。比较好的做法,不论有多少张图片,只需要创建3个imageView就够了。
本文介绍使用Collectionview来实现无限滚动的循环利用。它支持垂直和水平方向上的滚动。
CollectionCell的用法和tableViewCell的用法不太一样,CollectionCell
需要注册,告诉它这种标识对应的cell是什么类型的cell,如果缓存池中没有,那么它就检测当时这种标识注册的是什么类型的cell,就会自动创建这种类型的Cell。
2.实现步骤
  (1)向storyboard中添加一个UICollectionView,调整控件的宽高。
  (2)设置其宽高==一张图片的宽高==其一个cell的宽高
    设置cell的格子的大小。其默认为向上滚动的,调整为水平滚动。
    & &
  (3)连线,设置其数据源和代理
实现代码:
YYViewController.m
07-无限滚动(循环利用)
Created by apple on 14-8-3.
Copyright (c) 2014年 yangyong. All rights reserved.
9 #import "YYViewController.h"
11 @interface YYViewController ()&UICollectionViewDataSource,UICollectionViewDelegate&
12 @property (weak, nonatomic) IBOutlet UICollectionView *collectinV
16 @implementation YYViewController
18 - (void)viewDidLoad
[super viewDidLoad];
//注册cell
static NSString *ID=@"cell";
[self.collectinView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
27 #pragma mark- UICollectionViewDataSource
28 //一共多少组,默认为1组
29 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
33 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
return 16;
38 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
static NSString *ID=@"cell";
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
cell.backgroundColor=YYRandomC
46 #pragma mark-UICollectionViewDelegate
    界面展示:
    打印查看有没有实现cell的循环利用。
    可以看出,整个程序中只创建了两个cell。
  (4)展示图片,自定义cell(两种做法,可以使用xib也可以使用代码)。
    自定义一个cell,用来展示图片。
    实现代码:  YYimageCell.h文件
YYimageCell.h
07-无限滚动(循环利用)
Created by apple on 14-8-3.
Copyright (c) 2014年 yangyong. All rights reserved.
9 #import &UIKit/UIKit.h&
11 @interface YYimageCell : UICollectionViewCell
12 @property(nonatomic,copy)NSString *
YYimageCell.m文件
YYimageCell.m
07-无限滚动(循环利用)
Created by apple on 14-8-3.
Copyright (c) 2014年 yangyong. All rights reserved.
9 #import "YYimageCell.h"
11 @interface YYimageCell ()
12 @property(nonatomic,strong)UIImageView *imageV
14 @implementation YYimageCell
16 - (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self) {
UIImageView *imageView=[[UIImageView alloc]init];
[self addSubview:imageView];
self.imageView=imageV
28 -(void)setIcon:(NSString *)icon
_icon=[icon copy];
self.imageView.image=[UIImage imageNamed:icon];
34 -(void)layoutSubviews
[super layoutSubviews];
self.imageView.frame=self.
  YYViewController.m文件
YYViewController.m
07-无限滚动(循环利用)
Created by apple on 14-8-3.
Copyright (c) 2014年 yangyong. All rights reserved.
9 #import "YYViewController.h"
10 #import "YYimageCell.h"
12 #define YYCell @"cell"
14 @interface YYViewController ()&UICollectionViewDataSource,UICollectionViewDelegate&
15 @property (weak, nonatomic) IBOutlet UICollectionView *collectinV
19 @implementation YYViewController
21 - (void)viewDidLoad
[super viewDidLoad];
//注册cell
static NSString *ID=@"cell";
[self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
30 #pragma mark- UICollectionViewDataSource
31 //一共多少组,默认为1组
32 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
36 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
return 16;
41 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
static NSString *ID=@"cell";
YYimageCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:YYCell forIndexPath:indexPath];
cell.backgroundColor=YYRandomC
NSLog(@"%p,%d",cell,indexPath.item);
cell.icon=[NSString stringWithFormat:@"minion_%02d",indexPath.item+1];
51 #pragma mark-UICollectionViewDelegate
  界面实现:
  (5)细节处理
隐藏水平滚动条。
清除其颜色。
随笔 - 177
评论 - 1291iOS项目开发之实现无限轮播
iOS项目开发之实现无限轮播 ,
在实际的开发当中,会经常有界面需要实现图片的无限轮播这样的需求。比如新闻app,或者其他app的广告位
实现的方式有很多种,最先想动的一定是scrollVi
在实际的开发当中,会经常有界面需要实现图片的无限轮播这样的需求。比如新闻app,或者其他app的广告位
实现的方式有很多种,最先想动的一定是scrollView,但是其实scrollView实现起来并没有那么容易。这里,我用了一个较为取巧的办法,使用UICollectionView来实现无限轮播
无限轮播,通常就是图片的无限循环的播放。当到最后一个图片的时候,再次轮播时,显示第一个图片。
UICollectionView可以进行上下滚动,也可以进行左右滚动,所有这里我们只需要使用它,并且让它左右滚动即可
我这里将整个内容封装到一个View里面,使用起来较为简单,另外代码中注释也很清晰,这里不做阐述。
1?在主控制器中创建广告位
- (void)loadData
NSURL *dataUrl = [[NSBundle mainBundle] URLForResource:@"newses.plist" withExtension:nil];
NSArray *data = [NSArray arrayWithContentsOfURL:dataUrl];
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in data) {
AdvertModel *advert = [AdvertModel advertModelWithDict:dict];
[tempArray addObject:advert];
_advertData = tempA
- (void)createAdvertView
CGRect advertRect = CGRectMake(10, 40, 300, 130);
AdvertView *advertView = [[AdvertView alloc] initWithFrame:advertRect];
advertView.advertData = _advertD
[self.view addSubview:advertView];
2?广告位的View的实现
#import "AdvertView.h"
#import "AdvertModel.h"
#import "AdvertCell.h"
#define cellIdentifier @"advertcell"
@interface AdvertView() &UICollectionViewDataSource, UICollectionViewDelegate&
UICollectionView *_advertV
UIPageControl *_pageC
NSTimer *_
NSInteger _
NSInteger _
@implementation AdvertView
- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self) {
//1.设置宽高
_width = frame.size.
_height = frame.size.
//2.创建广告位和PageControl
[self createAdvertView];
[self createPageView];
//3.创建定时器
[self addTimer];
- (void)setAdvertData:(NSArray *)advertData
_advertData = advertD
[_advertView reloadData];
[_advertView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:100 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
创建广告位
- (void)createAdvertView
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.minimumLineSpacing = 0;
flowLayout.minimumInteritemSpacing = 0;
flowLayout.scrollDirection = UICollectionViewScrollDirectionH
flowLayout.itemSize = CGSizeMake(_width, _height);
CGRect advertRect = CGRectMake(0, 0, _width, _height);
_advertView = [[UICollectionView alloc] initWithFrame:advertRect collectionViewLayout:flowLayout];
[_advertView registerClass:[AdvertCell class] forCellWithReuseIdentifier:cellIdentifier];
_advertView.delegate =
_advertView.dataSource =
_advertView.pagingEnabled = YES;
_advertView.showsHorizontalScrollIndicator = NO;
_advertView.showsVerticalScrollIndicator = NO;
[self addSubview:_advertView];
创建页码的View
- (void)createPageView
UIPageControl *pageControl = [[UIPageControl alloc] init];
pageControl.center = CGPointMake(self.frame.size.width * 0.5, _height - 20);
pageControl.bounds = CGRectMake(0, 0, 100, 0);
pageControl.numberOfPages = 5;
pageControl.pageIndicatorTintColor = [UIColor blueColor];
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
[self addSubview:pageControl];
_pageControl = pageC
添加定时器
- (void)addTimer
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextAdvert) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
删除定时器
- (void)removeTimer
[_timer invalidate];
下一个广告
- (void)nextAdvert
CGFloat offset = _advertView.contentOffset.x + _
[_advertView setContentOffset:CGPointMake(offset, 0) animated:YES];
#pragma mark - UICollectionView的数据源和代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
//这里将数据放足够大,可以无限的轮播循环
return _advertData.count * 1000;
- (AdvertCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
AdvertCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.advert = _advertData[indexPath.item % 5];
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
//取出当前可见的单元格
NSIndexPath *visiablePath = [[collectionView indexPathsForVisibleItems] firstObject];
_pageControl.currentPage = visiablePath.item % 5;
#pragma mark 当拖拽时,暂时将定时器销毁
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
[self removeTimer];
#pragma mark 停止拖拽时,再次创建定时器
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
[self addTimer];
四、代码下载地址
iOS项目开发之实现无限轮播
CalendarStyle是iOS日历控件。项目主页:
PMCalendar是iOS上的一款日历控件,兼容iOS4.0.项目主页:
RBMenu是iOS菜单,灵感来源于MediumiOS应用。项目主页:
AlertPickerView是一个iOS的日期选择控件项目主页:
TDSemiModal在iOS上实现了半模态的日期选择器。项目主页:
NextiveJSON是一个支持iOS和OSX的JSON解析器。项目主页:
正则表达式在线测试工具
FaceYe @ 2015 &&&&
ICP备案号:粤ICP备1500070主题 : 怎么在 collectionView 滑动的时候添加动画
级别: 新手上路
可可豆: 12 CB
威望: 12 点
在线时间: 31(时)
发自: Web Page
来源于&&分类
怎么在 collectionView 滑动的时候添加动画&&&
我是用collectionView 使用横向滑动的 上面放的是全屏的图片 怎么在滑动的时候添加动画效果
级别: 新手上路
可可豆: 12 CB
威望: 12 点
在线时间: 31(时)
发自: Web Page
没人吗 自己顶一个十个字
级别: 侠客
可可豆: 121 CB
威望: 121 点
在线时间: 464(时)
发自: Web Page
全屏的图片?一个屏幕范围内只有一个cell?
级别: 新手上路
可可豆: 12 CB
威望: 12 点
在线时间: 31(时)
发自: Web Page
回 2楼(消逝De第六乐章) 的帖子
对,就像SCrollView 轮播图片一样 但是要有动画效果
级别: 侠客
可可豆: 121 CB
威望: 121 点
在线时间: 464(时)
发自: Web Page
回 3楼(wolumengru) 的帖子
你想加什么样的动画
级别: 新手上路
可可豆: 2 CB
威望: 2 点
在线时间: 2(时)
发自: Web Page
[UIView animateWithDuration:时间 animations:^{&&&&&&&&事件&&&&}]你是说的这个效果么 ?
关注本帖(如果有新回复会站内信通知您)
8*2-5 正确答案:11
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版}

我要回帖

更多关于 collectionview 滚动 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信