我有一个基于 Apple 的 PageControl 示例的应用程序.第一次加载视图时,滚动视图会加载第 0 页和第 1 页.每当启动滚动时,UIKit 应该调用 scrollViewDidScroll 方法对吗?
I have an app based on Apple's PageControl sample. The first time the view loads, the scroll view is loaded with page 0 and page 1. Whenever a scroll is initiated, the scrollViewDidScroll method should get called by UIKit correct?
从第 0 页开始滚动到第 1 页时,应用应加载第 1 页、第 1 页和第 1 页,(以防止滚动时闪烁).
When initiating a scroll from page 0 to page 1, the app should load page-1, page and page+1, (to prevent flashes during scrolling).
我的应用程序似乎调用了 scrollViewDidScroll
19 次,而我的 loadScrollViewWithPage:
方法在页面 0 和页面 1 中调用了 19 次,最终到达页面 1 和页面 2,然后它崩溃了.
My app seems to call scrollViewDidScroll
19 times and my loadScrollViewWithPage:
method 19 times each with page 0 and page 1, before it finally gets to page 1 and 2, then it crashes.
这些是方法:
- (void)scrollViewDidScroll:(UIScrollView *)sender {
NSLog(@"scrollviewdidscroll");
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed) {
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// A possible optimization would be to unload the views+controllers which are no longer visible
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= kNumberOfPages) return;
NSLog(@"page: %i", page);
// replace the placeholder if necessary
KeyboardViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[KeyboardViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
frame.size.height = scrollView.frame.size.height;
controller.view.frame = frame;
[scrollView setAutoresizesSubviews:YES];
[scrollView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
[scrollView addSubview:controller.view];
}
为什么 scrollViewDidScroll 会被调用这么多次?
Why would scrollViewDidScroll get called so many times?
谢谢
scrollViewDidScroll:
每次滚动边界改变时都会被调用.这意味着它在滚动期间以及开始时被调用.您可能想尝试 scrollViewWillBeginDragging:
.
scrollViewDidScroll:
gets called every time the scroll bounds change. This means it gets called during the scroll, as well as when it starts. You may want to try scrollViewWillBeginDragging:
instead.
这篇关于我的应用调用 scrollViewDidScroll 19 次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!