如何在视图之间创建一个包含空格的分页scrollView

rcm*_*ark 4 iphone objective-c scrollview uipagecontrol

我按照教程介绍了如何创建scrollView页面控件:http://www.iosdevnotes.com/2011/03/uiscrollview-paging/

本教程非常好,我实现了很好的代码.我的问题在这里:

我想在我的PageViews之间放一个空格,但是当它改变页面时,它会显示下一页中视图之间的空格.当我更改页面时,滚动必须在空格后停止.

我在这里更改了教程代码:

- (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);
}
Run Code Online (Sandbox Code Playgroud)

rob*_*off 9

听起来你想要在页面之间有一个"装订线",这样每个页面都会填充滚动视图,只有在用户拖动视图时才能看到装订线.例如,内置的照片应用可以执行此操作.

使space点滚动视图更宽.例如,如果你想在滚动视图看起来一样宽屏幕(320点),与项目之间的20点余量,然后进行滚动视图340点宽,额外的20点挂右边缘的屏幕.

- (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);
}
Run Code Online (Sandbox Code Playgroud)