I ve just finished implementing the infitine scroll for me.
In my Implementation I have UITableViewCell with a scrollView and Navigationbuttons. The scrollView contains x views all with the same width. views are alined horizontally and paging is enabled.
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
我的规律如下:
- In my initialization function I
- create all the views (for the scrollview) and
- put them into an array and
- add them to the scrollView
然后,我称之为一种功能,即按每个观点计算职位(每当你发现这一功能时,也需要打电话)。 它总是采用阵列的第一个要素,并将框架设定为(0,0,...,......),其次是(i*width,0,...,......)等等。 所谓的职能如下:
- (void)updateOffsetsOfViews{
int xpos = 0;
for (int i=0; i<[views count]; i++) {
UIImageView *_view = [views objectAtIndex:i];
CGRect aFrame = _view.frame;
aFrame.origin.x = xpos;
aFrame.origin.y = 0.0;
_view.frame = aFrame;
xpos += viewWidth;
}
float center = 0;
if(fmod([views count],2) == 1){
center = viewWidth * ([views count]-1)/2;
}else {
center = viewWidth * [views count]/2;
}
[scrollView setContentOffset:CGPointMake(center, 0)];
lastOffset = center;
}
然后(在初始化过程中暂停)我增加一名观察员。
[scrollView addObserver:self forKeyPath:@"contentOffset" options:0 context:nil];
每一次 观点的改变,我被称作“观察者”。
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
UIImageView *_viewFirst = (UIImageView *)[views objectAtIndex:0];
if ( fmod([scrollView contentOffset].x,viewWidth) == 0.0) {
if ([scrollView contentOffset].x > lastOffset) {
[views removeObjectAtIndex:0];
[views addObject:_viewFirst];
[self updateOffsetsOfViews];
}else if ([scrollView contentOffset].x < lastOffset) {
UIImageView *_viewLast = (UIImageView *)[views lastObject];
[views removeLastObject];
[views insertObject:_viewLast atIndex:0];
[self updateOffsetsOfViews];
}
}
}
而且,在交易或视Did Unload(取决于你如何执行)中,不会忘记去掉观察员。
[scrollView removeObserver:self forKeyPath:@"contentOffset"];
希望这一帮助,你可能会注意到一些间接费用,但在我的执行过程中,我也支持将5页(无限制的)一页和自动加薪等。 因此,你可能会看到一些可能被抛弃的东西。