2014-08-27 59 views
0

我試圖在_pageControl.currentPage處於確切頁碼時觸發一個方法,但問題在於當用戶滾動到另一個頁面時,編譯器在仍然滾動時檢測到新頁面!滾動完成後,我需要啓動我的方法!這是我的代碼:UIPageControl和scrollViewDidScroll問題

- (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年8月27日13:48:35.344 TADAEX [45131:60B] 1 2014年8月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 TADAEX [45131:60b] 2 2014-08-27 13:48:38.076 TADAEX [45131:60b] 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是在確切的頁碼。

+1

是的,這是正常的。 scrollViewDidScroll在用戶滾動時觸發。 – idmean 2014-08-27 09:24:53

+0

更好的選擇是使用if - else而不是Switch case。你可以這樣做,如果(頁面== 1) - > NSLog(@「1」); – Mohit 2014-08-27 09:33:59

回答

2

你可以做,在此委託方法

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
//Tells the delegate that the scroll view has ended decelerating the scrolling movement. 
} 
+0

謝謝.... .... – 2014-08-27 09:40:09

+0

歡迎......... – Rajesh 2014-08-27 09:41:58