【问题标题】:Issue with UIScrollView within UIScrollViewUIScrollView 中的 UIScrollView 问题
【发布时间】:2015-03-25 15:24:29
【问题描述】:

我的故事板中有一个UIScrollView,它可以垂直滚动。在这个滚动视图内部,我以编程方式创建了一个水平的UIScrollView

垂直的scrollView称为scroller。水平的 UIScrollView 称为 scrollInfo。由于某种原因,我无法在使用水平滚动信息时注册。关于为什么这不起作用的任何想法?

这是在我看来DidLoad:

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");
    }
}

【问题讨论】:

    标签: ios objective-c uiscrollview


    【解决方案1】:

    您似乎忘记将UIScrollViewDelegate 连接到您的滚动视图。完成此操作后,您最好使用 scrollview.tag 来识别哪个滚动视图调用委托

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      相关资源
      最近更新 更多