2012-07-04 61 views
0

我已經實施分頁使用UIScrollViewUIVIewUIControls在此UIVIew在每個頁面上。令人驚訝的是UIControlUIScrollView啓用分頁時沒有響應。以下是我的代碼。任何人都可以請告訴我如何獲得UIControlsUIScrollView工作啓用分頁?使用UIScrollView啓用分頁

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

    int totalPages = [[delegate sections] count] + 2; // 2 for startOfDayQuestions and endOfDayQuestions 

    int X = 45, Y = 20, H = 40, W = 680; 

    // start of the day questions 
    CGRect startFrame = scrollView.frame; 
    startFrame.origin.x = 0; 
    startFrame.origin.y = 0; 

    UIView *startPage = [[UIView alloc] initWithFrame:startFrame]; 

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(X, Y, W, H)]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.textColor = [UIColor blackColor]; 
    label.text = @"Text"; 
    label.font = [UIFont fontWithName:paragraph_font_name size:paragraph2_font_size]; 
    [startPage addSubview:label]; 

    Y += H; 

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(X, Y, W, H)]; 
    textField.borderStyle = UITextBorderStyleRoundedRect; 
    textField.font = [UIFont systemFontOfSize:15]; 
    textField.placeholder = @"enter text"; 
    textField.autocorrectionType = UITextAutocorrectionTypeNo; 
    textField.keyboardType = UIKeyboardTypeDefault; 
    textField.returnKeyType = UIReturnKeyDone; 
    textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  
    textField.delegate = self; 
    textField.userInteractionEnabled = YES; 
    [startPage addSubview:textField]; --> This UITextField doesn't respond when added to startPage and in turns scrollView but works fine if I add it to self.view! 

    // a page is the width of the scroll view 
    scrollView.pagingEnabled = YES; 
    scrollView.userInteractionEnabled = YES; 
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * totalPages, scrollView.frame.size.height); 
    scrollView.showsHorizontalScrollIndicator = NO; 
    scrollView.showsVerticalScrollIndicator = NO; 
    scrollView.scrollsToTop = NO; 
    scrollView.delegate = self; 

    pageControl.numberOfPages = totalPages; 
    pageControl.currentPage = 0; 

} 

- (void)scrollViewDidScroll:(UIScrollView *)sender 
{ 
    if (pageControlUsed) 
     return; 

    CGFloat pageWidth = scrollView.frame.size.width; 
    int page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
    pageControl.currentPage = page; 
} 

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 
{ 
    pageControlUsed = NO; 
} 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    pageControlUsed = NO; 
} 

- (IBAction)changePage:(id)sender 
{ 
    int page = pageControl.currentPage; 

    CGRect frame = scrollView.frame; 
    frame.origin.x = frame.size.width * page; 
    frame.origin.y = 0; 
    [scrollView scrollRectToVisible:frame animated:YES]; 

    pageControlUsed = YES; 
} 

回答

0

將沒有pagingEnabled的subScrollView添加到每個頁面,並將控件添加到subScrollView中!如果您將控件直接添加到啓用了分頁的滾動視圖中,似乎手勢識別器的第一響應者始終是滾動​​視圖,因此您的控件永遠不會獲得該事件並且行爲像禁用!