无限水平滚动UIScrollView [英] Infinite horizontal scrolling UIScrollView

查看:79
本文介绍了无限水平滚动UIScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIScrollView,其内容大小为1200x480。我有一些图像视图,其宽度加起来600.当向右滚动时,我只是增加内容大小并设置偏移量,以使一切顺利(然后我想添加其他图像,但这并不重要马上)。所以基本上,当前在视口中的图像保留在左侧的某个位置,最终将从超级视图中删除。

I have a UIScrollView whose content size is 1200x480. I have some image views on it, whose width adds up to 600. When scrolling towards the right, I simply increase the content size and set the offset so as to make everything smooth (I then want to add other images, but that's not important right now). So basically, the images currently in the viewport remain somewhere on the left, eventually to be removed from the superview.

现在,我向滚动的问题发生了剩下。我所做的是将图像移动到内容大小的末尾(因此为每个图像视图的origin.x添加600),然后相应地设置内容偏移。当手指在屏幕上并且用户拖动时(scrollView.isTracking = YES),它可以工作。当用户向左滚动并放开(scrollView.isTracking = NO)时,图像视图最终向右移动太快并几乎立即消失。有没有人知道我怎么能让图像移动得很好而且不会消失,即使用户没有手动拖动视图并且已经放开了?

Now, the problem that I have happens when scrolling towards the left. What I do is I move the images to the end of the content size (so add 600 to each image view's origin.x), and then set the content offset accordingly. It works when the finger is on the screen and the user drags (scrollView.isTracking = YES). When the user scrolls towards the left and lets go (scrollView.isTracking = NO), the image views end up moving too fast towards the right and disappear almost instantly. Does anyone know how I could have the images move nicely and not disappear even when the user's not manually dragging the view and has already let go?

这是我的水平拖动代码:

Here's my code for dragging horizontally:

-(void) scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint offset = self.scrollView.contentOffset;
    CGSize size = self.scrollView.contentSize;

    CGPoint newXY = CGPointMake(size.width-600, size.height-480);

    // this bit here allows scrolling towards the right
    if (offset.x > size.width - 320) {
        [self.scrollView setContentSize:CGSizeMake(size.width+320, size.height)];
        [self.scrollView setContentOffset: offset];
    }

    // and this is where my problem is:
    if (offset.x < 0) {
        for (UIImageView *imageView in self.scrollView.subviews) {
            CGRect frame = imageView.frame;
            [imageView setFrame:CGRectMake
                (frame.origin.x+newXY.x, frame.origin.y, 200, frame.size.height)];
        }
        [self.scrollView setContentOffset:CGPointMake(newXY.x+offset.x, offset.y)];
    } 
}

编辑:现在正在运行 - 我看了看在StreetScroller,它现在都很好。

This is now working - I had a look at StreetScroller and it's all good now.

但是,我现在想要放大滚动视图,但是从不调用viewForZoomingInScrollView。是不是可以放大内容大的滚动视图?

However, I now want to zoom in on the scrollview, but viewForZoomingInScrollView is never called. Is it not possible to zoom in on a scrollview with a large content size?

推荐答案

这里有一些方法。只需使用网站搜索...

There are some approaches floating around here. Just use the site search …

如果您想要Apple创建的更官方示例,请查看 StreetScroller演示。有关此示例的更多信息,请查看去年的WWDC会话编号。 104 高级滚动视图技术

If you want an more "official" example created by Apple take a look at the StreetScroller Demo. For some more information about this example take a look at last years WWDC session no. 104 Advanced Scroll View Techniques.

Github上还有一个名为 BAGPagingScrollView ,这是分页&无限,但它有一些你必须自己解决的错误,因为它不是在积极开发(特别是 goToPage:方法导致问题)。

There is also an UIScrollView subclass on Github called BAGPagingScrollView, which is paging & infinite, but it has a few bugs you have to fix on your own, because it's not under active development (especially the goToPage: method leads to problems).

这篇关于无限水平滚动UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆