2012-10-24 34 views
0

enter image description here如何通過觸摸從滾動視圖

,以查看我有3個tableviews在x = 0.0放置在滾動視圖中,x = 320.0並且x = 640.0。 當用戶水平滑動桌面視圖(因爲它們位於頂部)時,我想將滑動事件傳遞給其超級視圖,並且當用戶垂直滑動桌面視圖時,tableview必須垂直滾動。

我該如何做到這一點?

+0

更多詳情,請 – Mutawe

回答

2

UIScrollView通過觸摸,使用此代碼作爲一個類別:

@implementation UIScrollView (FixedApi) 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [[event allTouches] anyObject]; 

    NSLog(@"touch view = %@", [[touch view].class description]); 
    if ([[[touch view].class description] isEqualToString:@"UITableViewCellContentView"]) { 
     //To pass the touch to UITableViewCell 
    } else if ([[[touch view].class description] isEqualToString:@"UITableView"] && isHorizntalSCroll == true && pageNumber == 2) { 
     //To pass the touch to UITableView 

    } else if ([[[touch view].class description] isEqualToString:@"UIView"]) { 
     //To pass the touch to UIView 
    } else { 
    [self.superview touchesBegan:touches withEvent:event]; // or 1 nextResponder, depends 
    [super touchesBegan:touches withEvent:event]; 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    if (!self.dragging) [self.nextResponder.nextResponder touchesEnded:touches withEvent:event]; 
    [super touchesEnded:touches withEvent:event]; 
} 

@end 

決定垂直/水平滾動,你可以使用此代碼:

.h文件中<UIScrollViewDelegate>

BOOL pageControlIsChangingPage; 
CGPoint startPos; 
int  scrollDirection; 
int CX; //The width of the pages. 
BOOL isHorizntalSCroll; 
int pageNumber; 

.m文件

#pragma mark - 
#pragma mark UIScrollViewDelegate stuff 
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView { 
    if (scrollDirection==0){//we need to determine direction 
     //use the difference between positions to determine the direction. 
     if (abs(startPos.x-scrollView.contentOffset.x)<abs(startPos.y-scrollView.contentOffset.y)){   
      NSLog(@"Vertical Scrolling"); 
      scrollDirection=1; 
      isHorizntalSCroll = false; 
      [scrollView setPagingEnabled:NO]; 
     } else { 
      NSLog(@"Horitonzal Scrolling"); 
      scrollDirection=2; 
      isHorizntalSCroll = ture; 
      [scrollView setPagingEnabled:YES]; 
     } 
    } 

    if (scrollDirection==1) { 
     [scrollView setContentOffset:CGPointMake(startPos.x,scrollView.contentOffset.y) animated:NO]; 
    } else if (scrollDirection==2){ 
     [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x,startPos.y) animated:NO]; 
    } 
} 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView { 
    if (pageControlIsChangingPage) { 
     return; 
    } 

    CGFloat pageWidth = _scrollView.frame.size.width; 
    int page = floor((_scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
    pageControl.currentPage = page; 
    pageNumber = page; 
    NSLog(@"page number = %d",page); 
    if (page == 3 || page == 0) { 
     [scrollView setContentSize:CGSizeMake(CX, personalInfo.frame.size.height + 100)]; 
    } else { 
     [scrollView setContentSize:CGSizeMake(CX, [scrollView bounds].size.height)]; 
    } 

    pageControlIsChangingPage = NO; 
} 

#pragma mark - 
#pragma mark PageControl stuff 
- (IBAction)changePage:(id)sender { 
    /* 
    * Change the scroll view 
    */ 
    CGRect frame = scrollView.frame; 
    frame.origin.x = frame.size.width * pageControl.currentPage; 
    frame.origin.y = 0; 

    [scrollView scrollRectToVisible:frame animated:YES]; 

    /* 
    * When the animated scrolling finishings, scrollViewDidEndDecelerating will turn this off 
    */ 
    pageControlIsChangingPage = YES; 
} 

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollViews{ 
    startPos = scrollView.contentOffset; 
    scrollDirection=0; 
} 

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollViews willDecelerate:(BOOL)decelerate { 
    if (decelerate) { 
     scrollDirection=3; 
    } 
} 

就是這樣,

+0

這個答案使用私人API和全局狀態並覆蓋爲可疑邏輯流程中的所有滾動視圖UIScrollView的內部方法。這個答案是絕對可怕的,不應該被使用。 – AriX

0

通過IB或代碼禁用每個tableview的水平滾動,並將基本滾動視圖的contentsize設置爲960,heightOfTableview,您應該會很好......這應該會讓您繼續。

相關問題