2014-09-05 125 views
0

我的故事板中有一個UIScrollView垂直滾動。在這個scrollView裏面,我編程創建了一個水平的UIScrollViewUIScrollView內UIScrollView問題

垂直scrollView被稱爲滾動。水平的UIScrollView被稱爲scrollInfo。由於某些原因,我無法註冊使用水平scrollInfo的時候。任何想法,爲什麼這不工作?

這是我viewDidLoad中:

scrollInfo = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 
155,self.view.frame.size.width, self.view.frame.size.height/4)]; 
//This will create a scrollview of device screen frame 

scrollInfo.scrollEnabled = YES; 

NSInteger numberOfViews = 3; 
for (int i = 0; i < numberOfViews; i++) { 
    CGFloat xOrigin = i * self.view.frame.size.width; 
    UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, 
    self.view.frame.size.width, self.view.frame.size.height/4)]; 
    awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 
    alpha:1]; 
    [scrollInfo addSubview:awesomeView]; 
} 

// 3 views added horizontally to the UIScrollView by using xOrigin. 

scrollInfo.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, 
self.view.frame.size.height/4); 

[scroller addSubview:scrollInfo]; 
//Add scrollview to viewcontroller 

scrollInfo.pagingEnabled = YES; 

pageControl = [[UIPageControl alloc] init]; //SET a property of UIPageControl 
pageControl.frame = CGRectMake(0,200,self.view.frame.size.width, 
self.view.frame.size.height/8); 
pageControl.numberOfPages = 3; //as we added 3 diff views 
pageControl.currentPage = 0; 
[scroller addSubview:pageControl]; 

這裏是我的scrollViewDidScroll方法:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 

    if ([mainTableView isEqual: scrollView]) { 

     if (mainTableView.contentOffset.y > 0) { 
      // yourTableView is not on top. 
      NSLog(@"not top"); 
     } 

     else { 
      // yourTableView is already on top. 
      NSLog(@"is top"); 
      [UIView animateWithDuration:0.5f delay:0 
      options:UIViewAnimationOptionCurveLinear animations:^{ 
       scroller.contentOffset = CGPointMake(0, 0); 
      } completion:NULL]; 
     } 
    } 

    else { 

     CGFloat pageWidth = self.view.frame.size.width; 
     int page = floor((scrollInfo.contentOffset.x - pageWidth/2)/pageWidth) + 
     1; //this provide you the page number 
     pageControl.currentPage = page;// this displays the white dot as current page 
     NSLog(@"page"); 
    } 
} 

回答

0

看起來你忘了UIScrollViewDelegate連接到您的scrollviews。完成此操作後,最好使用scrollview.tag來識別哪個滾動視圖調用代理