如何让UIScrollView(pagingEnabled)每次移动1.2页? [英] How can I let UIScrollView (pagingEnabled) to move 1.2 page each time?
问题描述
确定。这是故事:
我有一个全屏的UIWebView,宽度= 768
I have a UIWebView in full screen, width = 768
它加载了几张图片并且这些图像在一行中,这意味着UIWebView将仅水平滚动。
It loads several images and those images are in one line, which means the UIWebView will scroll only horizontally.
现在,我将UIWebView中的UIScrollView设置为pagingEnabled = YES。因此,UIWebView上的滚动将逐页移动。
Now, I set the UIScrollView inside the UIWebView to pagingEnabled = YES. So the scroll on the UIWebView will move page by page.
问题是每个图像的宽度大约为900.我不会缩小它们,如果我滚动UIWebView,从第2页开始,将始终显示上一张图像的132点。这对我不好。
The problem is that every image's width is about 900. I won't scale them down and if I scroll the UIWebView, from the 2nd page on, always 132points of previous image will show. This is not good for me.
那么如何在UIWebView中操作UIWebView或UIScrollView,以便每个滚动页面移动900点,如果视图的帧宽是768?
约束是:
-
我无法更改其contentSize
I can't change its contentSize
我无法更改其界限或框架
I can't change its bounds or frame
我说这些约束因为UIWebView在加载内容时会根据自己的目的更改它们。
I say these constraints because UIWebView will change them on its own purpose when it loads contents.
有人想加入这个大脑风暴吗?
Anyone would like to join this brain storming?
谢谢
推荐答案
我想你将需要设置一个UIScrollView委托来跟踪开始和结束的触摸。更具体地说,scrollViewDidEndDragging:willDecelerate是我认为你需要使用的,因为它会在用户从设备上抬起手时触发。
I'm thinking you're going to need to set a UIScrollView delegate that tracks touches beginning and ending. More specifically, scrollViewDidEndDragging:willDecelerate is what I'm thinking you'll need to use, as it fires the instant a user lifts their hand from the device.
首先, pagingEnabled = NO似乎很明显,我们将不得不控制自己的分页。其中,我觉得不是太难了。跟踪scrollViewDidScroll:中滚动的方向,以便我们有一些全局集:
BOOL isGoingRight = YES或NO(NO表示最后一次移动在左边)
float PAGESIZE = 900.0;
First, pagingEnabled=NO seems obvious, we're going to have to control our own paging. Which, I don't feel is too tough. Track the direction of the scrolling in scrollViewDidScroll:, so that we have some global set: BOOL isGoingRight = YES or NO (NO means the last movement was to the left) float PAGESIZE = 900.0;
以下是我的scrollview委托方法处理分页的方式。
Here's how my scrollview delegate method would look like that handles paging.
- (void) scrollViewDidEndDragging:(UIScrollView*)_scrollView willDecelerate:(BOOL)willDecelerate {
float offsetProportion = _scrollView.contentOffset.x/_scrollView.frame.size.width;
//depending upon our direction, round up or down
int page = isGoingRight? ceilf(offsetProportion): floorf(offsetProportion);
//manually finish the scroll
[_scrollView scrollRectToVisible:CGRectMake(page*PAGESIZE, 0, _scrollView.frame.size.width, _scrollView.frame.size.height) animated:YES];
}
我没有测试过这个,你可能需要也可能不需要禁用触摸在点。
I didn't test this, you may or may not need to disable touches at points.
这篇关于如何让UIScrollView(pagingEnabled)每次移动1.2页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!