无限水平滚动 UIScrollView

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

问题描述

我有一个内容大小为 1200x480 的 UIScrollView.我有一些图像视图,其宽度加起来为 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.

现在,我在向左滚动时发生的问题.我所做的是将图像移动到内容大小的末尾(因此将 600 添加到每个图像视图的 origin.x),然后相应地设置内容偏移量.当手指在屏幕上并且用户拖动时它起作用(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 上还有一个 UIScrollView 子类叫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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Cancel current UIScrollView touch(取消当前 UIScrollView 触摸)
Scale image to fit screen on iPhone rotation(缩放图像以适应 iPhone 旋转时的屏幕)
How to make a screenshot of all the content of a Scrollview?(如何截取 Scrollview 的所有内容?)
Swift iOS Set scrollView constraint below navigation bar programmatically(Swift iOS以编程方式在导航栏下方设置滚动视图约束)
UILabel sizeToFit method not working properly(UILabel sizeToFit 方法无法正常工作)
Get the current UIScrollView scroll values on the iPhone?(获取 iPhone 上当前的 UIScrollView 滚动值?)