简体   繁体   English

UIScrollView自定义分页距离

[英]UIScrollView custom paging distance

I have a UIImage (Image's width is > 320px) in a UIScrollView (covers full width of screen) and want a paging effect. 我在UIScrollView中具有UIImage(图像的宽度> 320像素)(覆盖屏幕的整个宽度),并且需要分页效果。

All the tutorials/examples I found explain how to do it with multiple UIImages or UIVIews, but I only have 1 UIImage in the UIScrollView. 我发现的所有教程/示例都说明了如何使用多个UIImage或UIVIew,但是在UIScrollView中只有1个UIImage。 My aim is it to create a 'tape measure' like effect. 我的目标是创建类似“卷尺”的效果。 and the scrollview should 'page' on every marker of the tape? 滚动视图是否应在磁带的每个标记上“翻页”?

How can I do the paging on distances less the the width of the UIScrollView?! 如何在距离小于UIScrollView宽度的距离上进行分页?

I can't think of a cleaner solution than doing the math on the fly. 我想不出一个比做数学更干净的解决方案。 If the desired page length is 100px than "slide" the UIImage so that it stays relative to the actual page. 如果所需的页面长度比“滑动” UIImage的像素长100像素,则它相对于实际页面保持停留。 So something akin to: 所以类似于:

CGFloat pageWidth = 100;
int numPages = ceilf(myImageView.bounds.size.width / pageWidth);
scrollView.contentSize = CGSizeMake(numPages * scrollView.bounds.size.width,
                                  scrollView.bounds.size.height);

Then subclass the scroll view's layoutSubviews method: 然后将滚动视图的layoutSubviews方法子类化:

- (void)layoutSubviews {
    int numPages = ceilf(myImageView.bounds.size.width / pageWidth);
    CGRect visibleBounds = [self bounds];
    CGFloat start = visibleBounds.origin.x;
    CGFloat offset = start / pageWidth;
    myImageView.frame = CGRectMake(offset, myImageView.frame.origin.y,
                                   myImageView.frame.size.width,
                                   myImageView.frame.size.height);
}   

I haven't tested this at all but hopefully it gets you on a working track. 我根本没有测试过,但希望它能使您步入正轨。 What this should be doing is moving the image whenever you scroll so that it looks like each "page" only moves the image 100px. 应该做的是每当滚动时都移动图像,以使每个“页面”看起来只将图像移动100px。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM