如何设置ios uicollectionvieww 初始偏移

时间: 16:09 来源:转载 编辑:lucy
网友w8643351通过三星GALAXYNote4提问:1000瓦的电机 72v32安的电瓶 一个人能跑一百多公里吗 我的是鬼火电动车 想换新电瓶的热心网友1回答:这配置想跑100公里
那速度得多慢啊热心网友2回答:可以的网友u5b89uu822a9通过苹果iPhone6SPlus提问:我是72v32安的电池组6个电瓶,我再想串一个电瓶可以冯热心网友1回答:只要控制器耐压足够,就行。网友u4e5du6bdbu4e03通过OPPOR7Plus提问:72v20安的电瓶车能换72v32安的吗?热心网友1回答:没问题,可以的,电压一致,容量大一点时间长。热心网友2回答:最好不要换,会容易出问题的热心网友3回答:可以网友u68a6u60f3u54e5nice通过Iphone6s提问:本治电动车72V32安,海马电池。只能跑70公里电池就木电。是哪里有问题?热心网友1回答:电瓶使用半年以上的跑这么远差不多了网友w通过Iphone4s提问:我的电瓶车电机是32安的,现在是配的72V的电池,控制器是限流35,72V的。可以加一块电池吗热心网友1回答:可以,但最好不要出力热心网友2回答:不可以,会烧坏电机,要不然就换全套,划不来。网友u4e5du6bdbu4e03通过小米红米Note3提问:绿佳电动车旭派电池,72v,32安是几块电池热心网友1回答:72伏应该是72伏÷12伏=6块电池。网友zhacb通过三星GALAXYS6 提问:爱玛雪豹电池能不能装72V32安的?热心网友1回答:可以,
但32AH的电瓶体积很大,你必须要改装这车架,重新用铁条焊接电池框,才能安装进去。
最好是配置72v20ah锂电池,两组,锂电池体积小,可以安装进电池仓。
-----------
爱玛雪豹:72V1200W博世II代电机磁钢高40,15管...网友qiiqqqor通过三星2016版GALAXYA9提问:海宝72伏32安6个一组的电瓶要多少钱热心网友1回答:换的话650左右,直接买新的800多,72V需要6个单电池串联起来的,每个12V20AH电池140元左右网友greenleadacid通过三星GALAXYNote5提问:72伏50安电动车充电器可充72v32安的电瓶车吗热心网友1回答:不要用来充,除非是适合50AH的充电器可以应急给32AH的电瓶充充,不要长期用,夏季也不要用。热心网友2回答:可以的。充电时间会减少热心网友3回答:我立马的,我用32充20安可以充网友transcribe1567通过魅族MX5提问:72V32安雅迪电瓶车网购价多少热心网友1回答:这与电机功率有关。比如电机功率同样为48V500W、32Ah比20Ah理论行驶里程可多达33公里的样子。
  用20Ah理论行驶里程计算过程如下:
  1)匀速平均电流=功率/电压=500W/48V=10.4A;
  2)行驶时间=电池容量/平均电流=20AH/10...
你也可以分享到:用UICollectionView写的日历控件怎么自行设置可选日期_百度知道Pages: 1/2
主题 : 如何设置UICollectionView 初始偏移
级别: 新手上路
可可豆: 42 CB
威望: 60 点
在线时间: 149(时)
发自: Web Page
如何设置UICollectionView 初始偏移&&&
如题,我希望能够在进入UICollectionViewController的时候,collectionView的内容就已经滚动到我需要的位置,比如我用collectionView来写一个日历,有过去也有将来,那么我希望进去时能够直接定位到我需要的位置&&&&
级别: 新手上路
UID: 81995
可可豆: 67 CB
威望: 44 点
在线时间: 102(时)
发自: Web Page
collectionView有方法选择滚动到你所要的元素
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollP
级别: 新手上路
可可豆: 42 CB
威望: 60 点
在线时间: 149(时)
发自: Web Page
我希望直接默认滚动到需要的元素,这个方法是选择,而且滚动。但是如果在ViewDidLoad里面使用是无效的,而我在ViewDidApear里面使用,则交互显得不友好了
级别: 新手上路
UID: 81995
可可豆: 67 CB
威望: 44 点
在线时间: 102(时)
发自: Web Page
(BOOL)animated 把这个设置成NO 关掉动画 不知道会不会好些
级别: 新手上路
可可豆: 42 CB
威望: 60 点
在线时间: 149(时)
发自: Web Page
没用啊,如果写到viewDIdLoad里面,根本不执行,写到ViewDidApear里面就是一闪
级别: 新手上路
可可豆: 53 CB
威望: 53 点
在线时间: 49(时)
发自: Web Page
我也遇到了同样的问题,不知你解决了没有。谢谢
级别: 新手上路
可可豆: 42 CB
威望: 60 点
在线时间: 149(时)
发自: Web Page
解决了,在prepareForLayout里面写就可以了
级别: 新手上路
可可豆: 53 CB
威望: 53 点
在线时间: 49(时)
发自: Web Page
能详细告诉我这个函数怎么使用吗?
我的qq号,可以的话加qq聊。谢谢!!
级别: 骑士
可可豆: 1690 CB
威望: 1690 点
在线时间: 522(时)
发自: Web Page
回 6楼(danispring) 的帖子
求思路,这个控件有点怪!!
级别: 新手上路
可可豆: 12 CB
威望: 12 点
在线时间: 7(时)
发自: Web Page
楼主,麻烦请教一下最后怎么实现的?不盛感激
Pages: 1/2
关注本帖(如果有新回复会站内信通知您)
个人IDP证书一年费用? 正确答案:99美金
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版最终显示的效果图
1、UICollection的基本设置,并且创建一个继承自UICollectionViewFlowLayout的类。(不能是UICollectionViewLayout,否则全部都需要自定义)
2、在UICollectionViewFlowLayout类中完成四步
  - 1)重写prepareLayout方法进行基本的布局(cell在最左面的时候是在正中间),不能在init中布局,因为设置collectionView尺寸是在viewDidLoad中,而init在它之前调用,获得的collectionView的尺寸是空的
  - 2)重写shouldInvalidateLayoutForBoundsChange,当collectionView的显示范围发生改变的时候,让其内部重新布局(即让cell滚动起来)
  - 3)重写layoutAttributesForElementsInRect方法,让cell在左右滑动的时候,尺寸放大或缩小
  - 4)重写targetContentOffsetForProposedContentOffset方法,让最接近中心的cell在停在正中央。
代码如下:
viewContorller中:
1 #import "ViewController.h"
2 #import "ZWLineLayout.h"
3 @interface ViewController () &UICollectionViewDataSource&
5 @implementation ViewController
NSString *ZWCellID = @"cell";
7 - (void)viewDidLoad {
[super viewDidLoad];
//若为UICollectionViewLayout,itemSize和scrollDirection都需要自己写,下面的类继承自UICollectionViewLayout
ZWLineLayout *layout = [[ZWLineLayout alloc] init];
layout.itemSize = CGSizeMake(160, 160);
CGRect rect = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.width * 0.6);
UICollectionView *collection = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:layout];
collection.dataSource =
collection.backgroundColor = [UIColor greenColor];
[self.view addSubview:collection];
[collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ZWCellID];
20 #pragma mark - 数据源方法
21 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
return 10;
25 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ZWCellID forIndexPath:indexPath];
cell.backgroundColor = [UIColor orangeColor];
ZWLineLayout.m中
1 #import "ZWLineLayout.h"
3 @implementation ZWLineLayout
* 用来做布局的初始化操作(不建议在init方法中进行布局的初始化操作)
8 - (void)prepareLayout
[super prepareLayout];
//水平滚动
self.scrollDirection = UICollectionViewScrollDirectionH
CGFloat margin = (self.collectionView.frame.size.width - self.itemSize.width) / 2;
self.collectionView.contentInset = UIEdgeInsetsMake(0, margin, 0, margin);
* 当collectionView的显示范围发生改变的时候,是否需要重新刷新布局
* 一旦重新刷新布局,就会重新调用下面的方法:
* 1.prepareLayout
* 2.layoutAttributesForElementsInRect:方法
25 - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
return YES;
* 这个方法的返回值是一个数组(数组里面存放着rect范围内所有元素的布局属性)
* 这个方法的返回值决定了rect范围内所有元素的排布(frame)
35 //需要在viewController中使用上ZWLineLayout这个类后才能重写这个方法!!
36 - (NSArray&UICollectionViewLayoutAttributes *& *)layoutAttributesForElementsInRect:(CGRect)rect
//让父类布局好样式
NSArray *arr = [super layoutAttributesForElementsInRect:rect];
//计算出collectionView的中心的位置
CGFloat ceterX = self.collectionView.contentOffset.x + self.collectionView.frame.size.width * 0.5;
* 1.一个cell对应一个UICollectionViewLayoutAttributes对象
* 2.UICollectionViewLayoutAttributes对象决定了cell的frame
for (UICollectionViewLayoutAttributes *attributes in arr) {
//cell的中心点距离collectionView的中心点的距离,注意ABS()表示绝对值
CGFloat delta = ABS(attributes.center.x - ceterX);
//设置缩放比例
CGFloat scale = 1.1 - delta / self.collectionView.frame.size.
//设置cell滚动时候缩放的比例
attributes.transform = CGAffineTransformMakeScale(scale, scale);
* 这个方法的返回值,就决定了collectionView停止滚动时的偏移量
61 - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
// 计算出最终显示的矩形框
rect.origin.y = 0;
rect.origin.x = proposedContentOffset.x;
rect.size = self.collectionView.frame.
//获得super已经计算好的布局的属性
NSArray *arr = [super layoutAttributesForElementsInRect:rect];
//计算collectionView最中心点的x值
CGFloat centerX = proposedContentOffset.x + self.collectionView.frame.size.width * 0.5;
CGFloat minDelta = MAXFLOAT;
for (UICollectionViewLayoutAttributes *attrs in arr) {
if (ABS(minDelta) & ABS(attrs.center.x - centerX)) {
minDelta = attrs.center.x - centerX;
proposedContentOffset.x += minD
return proposedContentO
阅读(...) 评论()在写的一个App用到了UITableViewCell中嵌套UICollectionView的设计,原以为会很简单,但是后来发现还是问题多多的,例如DataSource的设置问题,还有我对重用中的一些理解的混乱,巴拉巴拉巴拉。所以就找到了这篇的原文。
原文:Putting a UICollectionView in a UITableViewCell
所以,你希望将collection view放进一个table view cell里面是吧?这听起来很简单对吧?好,做好这件简单的事情需要一点点准备工作。我们希望每个类结构清晰各司其职,那么UITableViewCell不能充当UICollectionView的DataSource和delegate(因为不分开的话会非常糟糕)。你可以下载这里的示例代码。
我们打算构建一个视图,结构如下图。每一个UITableViewCell会嵌套一个UICollectionView。(为了某些原因,我们到的collection view 会是自定义的子类。)每一个collection view 装着确定数目的cell,cell的数目由DataSource给出。
view的结构图
每一个 collection view与table view有同样的Datasource和delegate,自定义UICollectionView的子类也是我们需要做的。
diagram of DataSource&delegate
上面的图示向你展示了这个demo较为复杂的部分,如何将view controller看成UICollectionView的delegate。我们上面说到的子类继承了UICollectionView,我们给它一个index属性(property)。一半情况下,继承UICollectionView是比较少见的,不过在这里还是可以接受这种做法的。
将collection view 加到cell里面还是太早了,我们还需要做些工作。我们在cell 特定的初始化器中用标准的UICollectionViewFlowLayout实例化AFCollectionView的对象。
- (id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier
if (!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]))
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
layout.itemSize = CGSizeMake(44, 44);
layout.scrollDirection = UICollectionViewScrollDirectionH
self.collectionView = [[AFIndexedCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.showsHorizontalScrollIndicator = NO;
[self.contentView addSubview:self.collectionView];
我们在layoutSubviews里面调整collection view 的frame去适应并且填满cell。
-(void)layoutSubviews
[super layoutSubviews];
self.collectionView.frame = self.contentView.
接下来我们将在view colltroller里面设置我们的model。我们用多种UIColor来设置我们的模型,简单又明显。每一个cell将显示各不相同而且随机的颜色。
这个model是为了反映每个table view 和每个collection view的效果。我们首先用一个数组存储多个对象,每个对象反映一个table cell。然后这些对象它们自己也是数组,每个数组里的对象反映的是collection view cell里面的内容。
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
return self.colorArray.
接下来我们去实现UICollectionViewDataSource的方法。
-(NSInteger)collectionView:(AFIndexedCollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
NSArray *collectionViewArray = self.colorArray[collectionView.index];
return collectionViewArray.
-(UICollectionViewCell *)collectionView:(AFIndexedCollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath];
NSArray *collectionViewArray = self.colorArray[collectionView.index];
cell.backgroundColor = collectionViewArray[indexPath.item];
你会看到我们使用的index是collection view的属性,我们用它来查询到适合的model去配置我们的collection view cell。请注意,view controller不会存储任何指向collection view的引用,collection view们只属于UITableViewCell们。这是个好消息,因为它们都会被重用,这会节省我们的内存资源。
但是我们从哪里得到index?这也是我们需要做的。
-(void)tableView:(UITableView *)tableView willDisplayCell:(AFTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
[cell setCollectionViewDataSourceDelegate:self index:indexPath.row];
剩下要做的事情就是去“记住”每个collection view在其所在的table cell将要不显示(被滑动离开屏幕)时候的collection view内容的偏移量(collection view的content被滑动的距离),这样子就可以达到“记住”用户在不同table cell滑动collection view后的位置不在初始位置的效果了。我们这里利用NSMutableDictionary来记住我们内容的偏移量。
-(void)tableView:(UITableView *)tableView
willDisplayCell:(AFTableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
[cell setCollectionViewDataSourceDelegate:self index:indexPath.row];
NSInteger index = cell.collectionView.
CGFloat horizontalOffset = [self.contentOffsetDictionary[[@(index) stringValue]] floatValue];
[cell.collectionView setContentOffset:CGPointMake(horizontalOffset, 0)];
-(void)tableView:(UITableView *)tableView
didEndDisplayingCell:(AFTableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
CGFloat horizontalOffset = cell.collectionView.contentOffset.x;
NSInteger index = cell.collectionView.
self.contentOffsetDictionary[[@(index) stringValue]] = @(horizontalOffset);
content offset
以上就是我们全文的主要内容啦。不需要很多的代码,但是做好它需要在编码前进行稍微的设计结构。你可以在Github下载完整的代码。
最后还有作者的安利:)。真心希望你喜欢我的教程,我还推荐一下我的电子书UICollectionView: The Complete Guide。在书里面我会里里外外地向你介绍如何使用UICollectionView。你可以马上预定然后下载所以已经写出来的章节,心动不如行动。括弧笑。
热点阅读:
小主,按键盘右方向键 → 翻页可以跳过片头呢
本文标题:
原文链接:
和本文相似的内容:
编辑推荐 &&
日,佛山市发生了一起特殊凶杀案:犯罪嫌疑人李善周雇凶两名,将受害者张东成控制后,李善周亲自手持一柄尖刀,对着张东成的下体猛刺,活生生地将张东成的阴茎和

我要回帖

更多关于 uicollectionview滚动 的文章

 

随机推荐