内部UIScrollView

时间:2016-10-15 06:28:37

标签: ios objective-c uiscrollview

我正在尝试构建一个屏幕,其中包含2个UIScrollViews,1个主滚动视图,用作容器并垂直滚动(这很好)。

在主滚动视图内,是第二个滚动视图,用于显示不同的图像。这需要水平滚动,以便它可以翻页到其他图像,这些图像也会更新主滚动视图中显示的内容细节。

当试图让这两个功能一起工作时,分页功能不起作用,但是如果它在主滚动视图之外它将起作用但不会与其余内容一起滚动。

图像滚动视图也会在事件中被检测到,但不会显示任何滚动功能。

以下是我正在执行工作的3个功能。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Set up the Image Scroll View
    ImageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, screenWidth, homeImage)];
        ImageScrollView.scrollEnabled = YES;
        ImageScrollView.userInteractionEnabled=YES;
     [ImageScrollView setPagingEnabled:YES];
     [ImageScrollView setAlwaysBounceVertical:NO];
        ImageScrollView.delegate = self;

    // Set up the image array
        NSArray *imagesArray = [NSArray arrayWithObjects:@"staff1big.png", @"staff2big.png",    @"staff3big.png", nil];

    // Create each image subview
    for (int i = 0; i < [imagesArray count]; i++)
        {
                CGFloat xOrigin = i * ImageScrollView.frame.size.width;

            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0,  ImageScrollView.frame.size.width, ImageScrollView.frame.size.height)];
            [imageView setImage:[UIImage imageNamed:[imagesArray objectAtIndex:i]]];

            [ImageScrollView addSubview:imageView];
        }

    // Set the Content Size and Offset
        [ImageScrollView setContentSize:CGSizeMake(ImageScrollView.frame.size.width *       [imagesArray count], ImageScrollView.frame.size.height)];
        [ImageScrollView setContentOffset:CGPointMake(screenWidth*currentIndex, 0)];

        // Get the staff object
        dataSource = [DataSource dataSource];
        NSArray *staffArray = [dataSource getStaff];

    // Setup the Pager Control
        self.pageControl = [[UIPageControl alloc] init];

        NSInteger placement = (screenWidth/2)-50;

        self.pageControl.frame = CGRectMake(placement, homeImage-30, 100, 20);
        self.pageControl.numberOfPages = [staffArray count];
        self.pageControl.currentPage = currentIndex;

        [self setStaff:staffArray[currentIndex]];

    // Add the Main Scroll View
        MainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth,      screenHeight)];
        MainScrollView.delegate = self;
        MainScrollView.scrollEnabled = YES;
        MainScrollView.userInteractionEnabled=YES;

    // Add each object to the correct scroll view
        [ImageScrollView addSubview:self.pageControl];
        [MainScrollView addSubview:ImageScrollView];
        [MainScrollView addSubview:lineView];

        [self setDisplayContent]; // note the MainScrollView is added to the self.view in this method   along with setting the content size to screenWidth and height is determine by the generated     content.
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageWidth = ImageScrollView.frame.size.width;
    float fractionalPage = ImageScrollView.contentOffset.x / pageWidth;

    NSInteger page = lround(fractionalPage);
    currentIndex = page;
    self.pageControl.currentPage = page;
    [self displayContent];
}

- (void)displayContent
{
    int i = currentIndex;

    if (i < 0)
    {
        i = 0;
    } else if (i > 2) {
        i = 2;
    }

    [self setStaff:allStaff[i]];

    // remove all from view
    for(UIView *subview in [MainScrollView subviews]) {
        [subview removeFromSuperview];
    }

    NSArray *imagesArray = [NSArray arrayWithObjects:@"staff1big.png", @"staff2big.png", @"staff3big.png", nil];

    for (int i = 0; i < [imagesArray count]; i++)
    {
        CGFloat xOrigin = i * ImageScrollView.frame.size.width;

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, ImageScrollView.frame.size.width, ImageScrollView.frame.size.height)];
        [imageView setImage:[UIImage imageNamed:[imagesArray objectAtIndex:i]]];

        [ImageScrollView addSubview:imageView];
    }
    [ImageScrollView setContentSize:CGSizeMake(ImageScrollView.frame.size.width * [imagesArray count], ImageScrollView.frame.size.height)];
    [ImageScrollView setContentOffset:CGPointMake(screenWidth*currentIndex, 0)];

    // Get the staff
    dataSource = [DataSource dataSource];
    NSArray *staffArray = [dataSource getStaff];

    self.pageControl = [[UIPageControl alloc] init];

    NSInteger placement = (screenWidth/2) - 50;

    self.pageControl.frame = CGRectMake(placement, homeImage-30, 100, 20);
    self.pageControl.numberOfPages = [staffArray count];
    self.pageControl.currentPage = currentIndex;

    [self setStaff:staffArray[currentIndex]];

    [ImageScrollView addSubview:self.pageControl];
    [MainScrollView addSubview:ImageScrollView];
    [MainScrollView addSubview:lineView];

    [self setDisplayContent];
}

我确实需要重构一些代码以提高效率,但在这个阶段我只是试图让水平分页水平滚动。 如果有人能够帮助我指出如何解决这个问题的正确方向,我们将不胜感激。

这是我想要保留的UI的视觉效果,但是图像滚动水平。 Staff Display

0 个答案:

没有答案