我试图在_pageControl.currentPage
处于确切的页码时触发方法,但问题是当用户滚动到另一个页面时,编译器在滚动时检测到新页面!滚动完成后我需要触发我的方法!这是我的代码:
- (void)setupPageScroll {
_pageControl.currentPage = 0;
_pageControl.numberOfPages = 5;
[_scrollView setContentSize:CGSizeMake(_scrollView.frame.size.width * _pageControl.numberOfPages, _scrollView.frame.size.width)];
_scrollView.delegate = self;
[self createPageWithView:_logo forPage:0];
[self createPageWithView:_news forPage:1];
}
- (void)createPageWithView:(UIView *)iview forPage:(int)page
{
UIView *newView = [[UIView alloc] initWithFrame:
CGRectMake(_scrollView.frame.size.width * page, 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];
[newView addSubview:iview];
[_scrollView addSubview:newView];
}
- (IBAction)pageChanged:(id)sender {
CGRect pageRect = CGRectMake(_pageControl.currentPage * _scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height);
[_scrollView scrollRectToVisible: pageRect animated: YES];
}
- (void)scrollViewDidScroll: (UIScrollView *) sView
{
CGFloat offset = _scrollView.contentOffset.x;
CGFloat pageSize = _scrollView.frame.size.width;
int page = floor((offset + (pageSize/2)) / pageSize);
_pageControl.currentPage = page;
switch (_pageControl.currentPage) {
case 0:
NSLog(@"1");
break;
case 1:
NSLog(@"2");
break;
default:
break;
}
}
这是编译器的结果:
2014-08-27 13:48:35.344 TADAEX [45131:60b] 1 2014-08-27 13:48:35.361 TADAEX [45131:60b] 1 2014-08-27 13:48:35.378 TADAEX [45131:60b] 1 2014-08-27 13:48:37.248 TADAEX [45131:60b] 1 2014-08-27 13:48:37.265 TADAEX [45131:60b] 1 2014-08-27 13:48:37.282 TADAEX [45131:60b] 1 2014-08-27 13:48:37.299 TADAEX [45131:60b] 2 2014-08-27 13:48:38.076 TADAEX [45131:60b] 2 2014-08-27 13:48:38.076 TADAEX [45131:60b] 2 2014-08-27 13:48:38.076 TADAEX [45131:60b] 2 2014-08-27 13:48:38.076 TADAEX [45131:60b] 2 2014-08-27 13:48:38.076 TADAEX [45131:60b] 2
如您所见编译器重复提供NSLog
输出!当pageControl位于确切的页码时。
答案 0 :(得分:2)
您可以在此委托方法中执行此操作
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//Tells the delegate that the scroll view has ended decelerating the scrolling movement.
}