在启用分页的情况下使用 UIScrollView

Posted

技术标签:

【中文标题】在启用分页的情况下使用 UIScrollView【英文标题】:Using UIScrollView with paging enabled 【发布时间】:2012-07-04 09:02:04 【问题描述】:

我已经使用UIScrollView 实现了分页,并在每个页面的UIVIew 中添加了UIVIewUIControls。令人惊讶的是,当UIScrollView 启用分页时,UIControl 没有响应!以下是我的代码。谁能告诉我如何让UIControls 在启用分页的情况下处理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;

【问题讨论】:

【参考方案1】:

将没有 pagingEnabled 的 subScrollView 添加到每个页面并将控件添加到 subScrollView 有效!如果您在启用分页的情况下直接将控件添加到滚动视图,则手势识别器的第一响应者似乎始终是滚动​​视图,因此您的控件永远不会收到事件并且表现得像禁用了!

【讨论】:

以上是关于在启用分页的情况下使用 UIScrollView的主要内容,如果未能解决你的问题,请参考以下文章

在启用分页的情况下将表格视图添加到滚动视图。

UICollectionView 在启用分页的情况下不会滚动完整的 320 点

在启用分页的情况下使两个 UICollectionView 同步滚动

在启用分页的情况下检测滚动视图中的 y 偏移量何时发生变化

在启用本机分页的情况下查看 UICollectionView 中的上一个/下一个单元格?

尝试在启用分页的情况下访问 VESA LFB 时出现页面错误