通常,教程UIScrollView
谈论多个页面与屏幕的大小。
从屏幕的第一页开始,第二页出现在右侧:
当滚动时,第二页也会发生同样的事情:第一页和第三页出现在左侧/右侧。
这个滚动视图是“可实现的”吗?有一种方法可以使用页面但不是整个屏幕宽度?
通常,教程UIScrollView
谈论多个页面与屏幕的大小。
从屏幕的第一页开始,第二页出现在右侧:
当滚动时,第二页也会发生同样的事情:第一页和第三页出现在左侧/右侧。
这个滚动视图是“可实现的”吗?有一种方法可以使用页面但不是整个屏幕宽度?
尝试设置您的滚动视图内容大小
scrollView.contentSize = CGSizeMake(W, 1);
请记住,“W”必须是大于滚动视图宽度的数字才能水平滚动。1 是您设置内容大小高度的位置,如果您希望它垂直滚动,您可以使“1”大于滚动视图的高度。
是的,您可以这样做,只需将滚动视图的大小更改为您想要的大小,内容大小为滚动视图的 (numberOfPages * width) 还启用分页
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(numberOfPages * scrollView.frame.size.width, 1);
有关更多想法,请参阅此示例项目,只需更改 xib 中的滚动视图大小 :) 希望它有所帮助 :)
您需要实现一个滚动视图容器(例如 ScrollViewContainer)来处理-hitTest:withEvent:
方法:
ScrollViewContainer.h:
@interface BookAlbumScrollViewContainer : UIView {
UIScrollView * theScrollView_;
}
@property (nonatomic, retain) UIScrollView * theScrollView;
@end
ScrollViewContainer.m:
@implementation BookAlbumScrollViewContainer
@synthesize theScrollView = theScrollView_;
//...
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView * child = [super hitTest:point withEvent:event];
if (child == self)
return self.theScrollView;
return child;
}
@end
这里是一个相关的 QA on-hitTest:withEvent:
method。
设置滚动视图时,将其设置为:
CGFloat yourScrollViewPageWidth = 280.f;
CGRect yourScrollViewFrame = CGRectMake((320.f - yourScrollViewPageWidth) / 2.f, 0.f, yourScrollViewPageWidth, 200.f);
CGRect theScrollViewContainerFrame = CGRectMake(0.f, 0.f, 320.f, 200.f);
// Your scroll view
yourScrollView_ = [[UIScrollView alloc] initWithFrame:yourScrollViewFrame];
//...
[self.view addSubview:bookScrollView_];
// Your scroll view container to handle touches, it should be over |yourScrollView_|
theScrollViewContainer_ = [ScrollViewContainer alloc];
[theScrollViewContainer_ initWithFrame:theScrollViewContainerFrame];
[theScrollViewContainer_ setBookAlbumScrollView:yourScrollView_];
[self.view theScrollViewContainer_];