UIScrollView没有分页正确的距离

时间:2011-04-22 08:57:43

标签: iphone ios ipad uiscrollview paging

我正在尝试使用分页设置UIScrollView。非常简单 - 从 Scroller 示例代码中借用了大部分初始代码。这是拥有滚动视图的控制器接口的代码:

#import <UIKit/UIKit.h>

@interface BookViewController : UIViewController <UIScrollViewDelegate> {
    IBOutlet UIScrollView *pageScrollView;    
}

@property (nonatomic, retain) IBOutlet UIScrollView *pageScrollView;

@end

实施:

#import "BookViewController.h"

const CGFloat kScrollObjHeight  = 513.0;
const CGFloat kScrollObjWidth   = 1024.0;
const NSUInteger kNumImages     = 2;

@implementation BookViewController

@synthesize pageScrollView;

- (id)initWithNibName:(NSString *)nibNameOrNil 
               bundle:(NSBundle *)nibBundleOrNil
{
    return [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
}

- (void)dealloc
{
    [pageScrollView release];
    pageScrollView = nil;
    [super dealloc];    
}

- (void)layoutScrollImages
{
    UIView *view = nil;
    NSArray *subviews = pageScrollView.subviews;

    // Reposition all image subviews in a horizontal serial fashion
    CGFloat curXLoc = 0;
    int i = 1;
    for (view in subviews)
    {
        if ([view isKindOfClass:[UIView class]] && view.tag > 0)
        {
            CGRect frame = view.frame;
            frame.origin = CGPointMake(curXLoc, 0);
            view.frame = frame;

            curXLoc += (kScrollObjWidth);

            NSLog(@"layoutScrollImages: (%d) frame.size - %f x %f",
                  i, view.frame.size.width, view.frame.size.height);
            NSLog(@"layoutScrollImages: (%d) frame.origin - (%f, %f)", 
                  i, view.frame.origin.x, view.frame.origin.y);
            i++;
        }
    }

    // Set the content size so it can be scrollable.
    [pageScrollView setContentSize:CGSizeMake((kNumImages * kScrollObjWidth),
                                           pageScrollView.bounds.size.height)];
    NSLog(@"scrollView.contentSize - %f x %f", 
          pageScrollView.contentSize.width, pageScrollView.contentSize.height);
    NSLog(@"scrollView.frame.size - %f x %f", 
          pageScrollView.frame.size.width, pageScrollView.frame.size.height);
    NSLog(@"scrollView.bounds.size - %f x %f", 
          pageScrollView.bounds.size.width, pageScrollView.bounds.size.height);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];

    [pageScrollView setBackgroundColor:[UIColor blackColor]];
    [pageScrollView setCanCancelContentTouches:NO];
    pageScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    pageScrollView.clipsToBounds = YES;
    pageScrollView.scrollEnabled = YES;
    pageScrollView.pagingEnabled = YES;

    for (NSUInteger i = 1; i <= kNumImages; i++) {
        UIView *subview = [[UIView alloc] initWithFrame:pageScrollView.bounds];
        subview.tag = i;

        CGFloat r, g, b;
        r = (CGFloat)random() / INT32_MAX;
        g = (CGFloat)random() / INT32_MAX;
        b = (CGFloat)random() / INT32_MAX;

        subview.backgroundColor = [UIColor colorWithRed:r 
                                                  green:g 
                                                   blue:b 
                                                  alpha:1.0];

        NSLog(@"viewDidLoad: (%d) frame.size - %f x %f", 
              i, subview.frame.size.width, subview.frame.size.height);
        [pageScrollView addSubview:subview];
        [subview release];
    }

    [self layoutScrollImages];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    pageScrollView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:
        (UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidEndDecelerating: contentOffset - (%f, %f)",
          scrollView.contentOffset.x, scrollView.contentOffset.y);
}

@end

问题是,当我在此滚动视图中滚动时,调页距离比其中一个子视图的宽度短。换句话说,当我“向右翻页”时,我正在向右翻页一页,少于40px。这是NSLog()语句的(删节)输出:

viewDidLoad: (1) frame.size - 1024.000000 x 513.000000
viewDidLoad: (2) frame.size - 1024.000000 x 513.000000
layoutScrollImages: (1) frame.size - 1024.000000 x 513.000000
layoutScrollImages: (1) frame.origin - (0.000000, 0.000000)
layoutScrollImages: (2) frame.size - 1024.000000 x 513.000000
layoutScrollImages: (2) frame.origin - (1024.000000, 0.000000)
scrollView.contentSize - 2048.000000 x 513.000000
scrollView.frame.size - 1024.000000 x 513.000000
scrollView.bounds.size - 1024.000000 x 513.000000

除非我遗漏了某些内容,否则每个子视图和滚动视图frame的大小完全相同,滚动视图的contentSize高度相同,但宽度恰好是其两倍。就定位而言,第二个滚动视图正确移位。我可能会缺少什么?

编辑:我实施了UIScrollViewDelegate方法,

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

现在,在左/右向右滑动到第二页后,该方法中的NSLog()调用给出了以下内容:

scrollViewDidEndDecelerating: contentOffset - (980.000000, 20.000000)

所以,这是一个完整的“页面”的分页44px(我在最初的问题中转换了一些内容并说它滚动得太远了),然后向下移动了20px - 这给了什么?

0 个答案:

没有答案