【发布时间】:2016-05-29 04:10:47
【问题描述】:
如何使用 NSTimer 在水平位置自动滚动 UIcollectionView
我正在处理下面的项目源代码:
- (void)viewDidLoad {
[super viewDidLoad];
self.endPoint = CGPointMake(0, self.collectionView.frame.size.width);
self.scrollingPoint = CGPointMake(0, 0);
self.scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:0.015 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}
- (void)onTimer {
self.collectionView.contentOffset = self.scrollingPoint;
if (CGPointEqualToPoint(self.scrollingPoint, self.endPoint)) {
[self.scrollingTimer invalidate];
}
self.scrollingPoint = CGPointMake(self.scrollingPoint.x+1, 0);
}
用上面的代码试过了,但对我不起作用。
【问题讨论】:
-
你不应该为此使用 NSTimer。 CADisplayLink 链接到显示器,因此将为您提供更新屏幕相关事件的更好方法。这在 WWD 2015 或 WWDC 2014 的 scrollView 会话中得到了很好的解释。
-
先尝试设置数据源,然后在您的 viewdidload 中重新加载或尝试在 viewwillappear 中显示。然后调用你的计时器
标签: ios objective-c swift cocoa-touch