2

我正在使用 scrollViewWillEndDragging:withVelocity:targetContentOffset: 将我的滚动视图捕捉到页面。我根据速度自定义了行为。它工作正常,只是滚动视图在大偏移量下过早减速。contentOffset 最终到达了正确的位置,但是当滚动即将结束时,速度变得如此之低,以至于大约需要 3-4 秒才能到达目标。当尝试滚动 3-4 个全屏页面时,问题变得更加明显。下面的代码来自一个独立项目(创建新的 Single View 项目,在情节提要中添加全屏滚动视图,仅设置 iPad,横向模式)。

仅供参考,更改 decelerateRate 并不能解决问题(它只会使滚动速度变慢/变快)。

#import "ViewController.h"

@implementation ViewController
@synthesize scrollView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    pages_ = 30;
    UIView* arr[pages_];
    for (int i = 0; i < pages_; ++i) {
        CGRect frame = CGRectMake(1024 * i, 0, 1024, 768);
        arr[i] = [[UIView alloc] initWithFrame:frame];
        [scrollView addSubview:arr[i]];
        arr[i].backgroundColor = (i%2) ? [UIColor redColor] : [UIColor greenColor];
    }

    scrollView.contentSize = CGSizeMake(1024 * pages_, 768);
    scrollView.delegate = self;
    scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
}

- (void)scrollViewWillEndDragging:(UIScrollView *)sv
                     withVelocity:(CGPoint)velocity
              targetContentOffset:(inout CGPoint *)targetContentOffset {
    CGFloat pageWidth = scrollView.frame.size.width;
    unsigned int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

    int scrollToPage = page + velocity.x*1.5;
    scrollToPage = MAX(0, MIN(scrollToPage, (int)pages_ - 1));
    NSLog(@"Scrolling by %d pages", abs(page-scrollToPage));

    targetContentOffset->x = scrollView.frame.size.width * scrollToPage;
    targetContentOffset->y = 0;
    NSLog(@"Scrolling to offset %f", targetContentOffset->x);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)sv {
    NSLog(@"scrollViewDidEndDecelerating");
}

@end
4

1 回答 1

0

After talking to some Apple engineers at WWDC, this seems to be a bug in iOS. It's probably not fixed in iOS 7

于 2013-07-03T20:29:46.113 回答