English 中文(简体)
缩略语 观点。 任何关于执行“有限”的roll/动物的想法?
原标题:UIScrollView. Any thoughts on implementing "infinite" scroll/zoom?
  • 时间:2009-09-29 17:32:09
  •  标签:

因此,UITableView基本上支持“无限”滚动。 可能有一个限制,但该cker子可以停泊。 我想用美国国际法学会的意见来解释这一行为,但有两个根本障碍:

1) scrollView.contentSize is fixed at creation time. 2) zooming can blow any lazy-loading scheme all to hell since it can cause infinte data explosion.

是否有其他人认为这一想法? 我知道,我知道,我们基本上谈论在这里重新绘制谷歌地图。 任何见解都会受到高度赞赏。

Cheers, Doug

最佳回答

虽然不可能有真正无限的UIScroll 意见,但可以采用一些简单的方法来效仿这种行为。

  1. Handling the fixed contentSize: have some fixed-size view handled by your scroll view, and at launch or instantiation, set the content offset so that you re seeing the middle of the handled view. Then just watch the content offset (using KVO or some other method), and if you near any edge, update the content of the view with a new set of content (offset appropriately) and reset the scroll view s contentOffset property to be back in the middle.
  2. Handling zooming: do something similar, only this time watch the zoom factor on the scroll view. Whenever it gets to a certain point, do some manipulation to whatever data you re presenting so that it appears zoomed, then reset the zoom factor to 1.0. For example, if you re scrolling an image and it gets zoomed to appear twice as large, programmatically apply some kind of transform to make the image twice as large, then reset the scroll view s zoom factor to 1.0. The image will still appear zoomed in, but the scroll view will be able to continue zooming in further as necessary. (Google Maps takes this one step further where it lazy-loads more detailed views as the user zooms - you may or may not choose to implement this.)
问题回答

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;

我的规律如下:

  1. In my initialization function I
    • create all the views (for the scrollview) and
    • put them into an array and
    • add them to the scrollView
  2. 然后,我称之为一种功能,即按每个观点计算职位(每当你发现这一功能时,也需要打电话)。 它总是采用阵列的第一个要素,并将框架设定为(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;
    }
    
  3. 然后(在初始化过程中暂停)我增加一名观察员。

    [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];
            }
        }
    }
    
  4. 而且,在交易或视Did Unload(取决于你如何执行)中,不会忘记去掉观察员。

    [scrollView removeObserver:self forKeyPath:@"contentOffset"];   
    

希望这一帮助,你可能会注意到一些间接费用,但在我的执行过程中,我也支持将5页(无限制的)一页和自动加薪等。 因此,你可能会看到一些可能被抛弃的东西。

铭记在发卷时,contentOffset 多次变化,不仅逐页改动,而且每升一页。

也许将<代码>contentSize设定到某些巨型价值上,然后将数量有限的基本观点推向轨道上,因为在蒂尔格样本中,这种观点将会 do。

为了减少最终达到顶点的可能性,并且必须 abrupt然地更新观点(取消目前启动的任何滚动),可以不时地改变这种观点。

不管怎么说,这是我要尝试的。





相关问题
热门标签