根据Apple iOS Scroll View Programming Guide现在..
要创建丰富的用户体验,您可能希望在应用程序中嵌套滚动视图。在 iOS 3.0 之前,即使不是不可能,也很难做到这一点。在 iOS 3.0 中,完全支持此功能并自动运行。
所以嵌套滚动视图非常好> iOS 3。为了回答您的问题,我将滚动视图 C 作为子视图添加到“容器”UIView - 在将该容器视图添加到滚动视图 B 之前。似乎停止反弹将父滚动视图拉下,尽管我确实必须非常用力和快速地滑动才能使其在没有容器视图的情况下表现出您所描述的行为。
@interface TiledScrollView : UIScrollView @end
@implementation TiledScrollView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
self.layer.borderColor = [UIColor whiteColor].CGColor;
self.layer.borderWidth = 10.f;
}
return self;
}
- (void)setContentSize:(CGSize)contentSize
{
[super setContentSize:contentSize];
BOOL horizontal = contentSize.width > self.frame.size.width;
if(horizontal)
{
CGFloat col = self.contentSize.width / 10.f;
for (int i = 0; i < 10; i++)
{
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(i * col, 0, col, self.contentSize.height)];
view.backgroundColor = [UIColor colorWithHue:(arc4random() % 100) / 100.f saturation:1.f brightness:1.f alpha:1.f];
[self addSubview:view];
}
}
else
{
CGFloat row = self.contentSize.height / 10.f;
for (int i = 0; i < 10; i++)
{
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, i * row, self.contentSize.width, row)];
view.backgroundColor = [UIColor colorWithHue:(arc4random() % 100) / 100.f saturation:1.f brightness:1.f alpha:1.f];
[self addSubview:view];
}
}
}
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect bounds = [UIScreen mainScreen].bounds;
TiledScrollView * A = [[TiledScrollView alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height)];
TiledScrollView * B = [[TiledScrollView alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height / 2.f)];
TiledScrollView * C = [[TiledScrollView alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height / 4.f)];
[A setContentSize:CGSizeMake(bounds.size.width * 4, bounds.size.height)];
[B setContentSize:CGSizeMake(bounds.size.width, bounds.size.height * 4)];
[C setContentSize:CGSizeMake(bounds.size.width, bounds.size.height * 4)];
UIView * container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height / 4.f)];
[container addSubview:C];
[B addSubview:container];
[A addSubview:B];
[self.view addSubview:A];
}
@end