UIScrollView 自定义分页 [英] UIScrollView Custom Paging
问题描述
我的问题与我正在尝试使用滚动器实现的自定义分页形式有关,如果您首先考虑老虎机中实现的滚动视图类型,这会更容易形象化.
My question has to do with a custom form of paging which I am trying to do with a scroller, and this is easier to visualise if you first consider the type of scroll view implemented in a slot machine.
所以说我的 UIScrollView 的宽度为 100 像素.假设它包含 3 个内部视图,每个视图的宽度为 30 像素,因此它们之间的宽度为 3 像素.我想要实现的分页类型是每个页面都是我的一个视图(30 像素),而不是滚动视图的整个宽度.
So say my UIScrollView has a width of 100 pixels. Assume it contains 3 inner views, each with a width of 30 pixels, such that they are separated by a width of 3 pixels. The type of paging which I would like to achieve, is such that each page is one of my views (30 pixels), and not the whole width of the scroll view.
我知道通常情况下,如果视图占据了滚动视图的整个宽度,并且启用了分页,则一切正常.但是,在我的自定义分页中,我还希望滚动视图中的周围视图也可见.
I know that usually, if the view takes up the whole width of the scroll view, and paging is enabled then everything works. However, in my custom paging, I also want surrounding views in the scroll view to be visible as well.
我该怎么做?
推荐答案
我刚刚为另一个项目做了这个.您需要做的是将 UIScrollView 放入 UIView 的自定义实现中.我为此创建了一个名为 ExtendedHitAreaViewController 的类.ExtendedHitAreaView 覆盖了 hitTest 函数以返回它的第一个子对象,这将是您的滚动视图.
I just did this for another project. What you need to do is to place the UIScrollView into a custom implementation of UIView. I created a class for this called ExtendedHitAreaViewController. The ExtendedHitAreaView overrides the hitTest function to return its first child object, which will be your scroll view.
您的滚动视图应该是您想要的页面大小,即 30px 且 clipsToBounds = NO.扩展的命中区域视图应该是您想要可见的区域的完整大小,并且 clipsToBounds = YES.
Your scroll view should be the page size you want, i.e., 30px with clipsToBounds = NO. The extended hit area view should be the full size of the area you want to be visible, with clipsToBounds = YES.
将滚动视图作为子视图添加到扩展命中区域视图,然后将扩展命中区域视图添加到您的视图控制器的视图中.
Add the scroll view as a subview to the extended hit area view, then add the extended hit area view to your viewcontroller's view.
@implementation ExtendedHitAreaViewContainer
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if ([self pointInside:point withEvent:event]) {
if ([[self subviews] count] > 0) {
//force return of first child, if exists
return [[self subviews] objectAtIndex:0];
} else {
return self;
}
}
return nil;
}
@end
这篇关于UIScrollView 自定义分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!