我有一个UICollectionView
单行,每个单元格都有不同的width
,(取决于某些条件)。每个cell
都有一个float timeInSeconds
属性与之关联。
我希望水平自动滚动UICollectionView
,以便细胞分别传入与该细胞相关联的timeInSeconds
。
我尝试使用NSTimer
并更改contentOffset
,但不按我的意愿工作。
代码:
//this method is called which fires the `NSTimer`
- (void)configAutoscrollTimer {
w = self.messagesCollectionView.contentOffset.x;
autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}
- (void)onTimer {
[self autoScrollView];
}
- (void)autoScrollView {
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:_currentPlayIndex inSection:0];
UICollectionViewCell *cell = [_messagesCollectionView cellForItemAtIndexPath:lastIndexPath];
w += ([(NSNumber*)_messagesArray[lastIndexPath.row][@"duration"] floatValue])/cell.bounds.size.width;
CGPoint offsetPoint = CGPointMake(w, 0);
self.messagesCollectionView.contentOffset = offsetPoint;
}
我认为问题在于X
偏移计算,根据某些公式,它应该移动多少步。我的公式失败了:(
请根据与单元格相关的时间指导如何实现自动滚动。谢谢!
答案 0 :(得分:0)
在物理学中space = velocity * time
,所以w
应增加velocity * time
(每0.01秒增量时间)。
如果单元格的可访问持续时间保存在_messagesArray[lastIndexPath.row][@"duration"]
中,您应该可以将velocity
计算为space/time
。
因此,增量应该像这样定义
float v = self.messagesCollectionView.frame.size.width / ([(NSNumber*)_messagesArray[lastIndexPath.row][@"duration"] floatValue]);
w += v * 0.01f;
(当然v
每个单元格应该只计算一次)
让我知道它是否有效或我的答案应该被编辑
修改强>
基于当前单元格宽度的速度
float v = cell.bounds.size.width / ([(NSNumber*)_messagesArray[lastIndexPath.row][@"duration"] floatValue]);
w += v * 0.01f;