如何在视图之间创建具有空格的分页scrollView [英] How to create a paging scrollView with space between views
问题描述
我遵循了有关如何创建scrollView页面控件的教程: http:// www.iosdevnotes.com/2011/03/uiscrollview-paging/
I followed the tutorial about how to create a scrollView Page Control: http://www.iosdevnotes.com/2011/03/uiscrollview-paging/
本教程非常好,我很好地实现了代码。在这里我的问题是:
This tutorial is really good and I implement well the code. Here my question:
我想在我的PageViews之间放置一个空格,但是当它改变页面时,它会显示下一页中视图之间的空格。当我更改页面时,滚动必须在空格后停止。
I want to put a space between the my PageViews, but when it change the page it show the space between the views in the next page. The scroll must stop after the space when I change the page.
我在这里更改了教程代码:
I changed the tutorial code here:
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
#define space 20
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = (self.scrollView.frame.size.width + space) * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count+ space*(colors.count-1), self.scrollView.frame.size.height);
}
推荐答案
听起来你想要一个页面之间的装订线,以便每个页面填充滚动视图,并且只有在用户拖动视图时才能看到装订线。例如,内置的照片应用会执行此操作。
It sounds like you want a "gutter" between the pages, so that each page fills the scroll view and the gutter is only visible while the user is dragging the view. The built-in Photos app does this, for example.
通过空格
点使您的滚动视图更宽。例如,如果您希望滚动视图看起来与屏幕一样宽(320点),项目之间的边距为20点,则滚动视图为340点宽,额外的20点悬挂在右边缘屏幕。
Make your scroll view wider by space
points. For example, if you want the scroll view to appear to be as wide as the screen (320 points), with a 20 point margin between items, then make the scroll view 340 points wide, with the extra 20 points hanging off the right edge of the screen.
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
#define kGutterWidth 20
UIScrollView *scrollView = self.scrollView;
CGRect scrollViewFrame = scrollView.frame;
scrollViewFrame.size.width += kGutterWidth;
scrollView.frame = scrollViewFrame;
CGSize scrollViewSize = scrollView.bounds.size;
for (int i = 0; i < colors.count; i++) {
CGRect frame = CGRectMake(scrollViewSize.width * i, 0,
scrollViewSize.width - kGutterWidth, scrollViewSize.height);
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[scrollView addSubview:subview];
[subview release];
}
scrollView.contentSize = CGSizeMake(
colors.count * scrollViewSize.width,
scrollViewSize.height);
}
这篇关于如何在视图之间创建具有空格的分页scrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!