0

我正在尝试创建一个 UIScrollView ,它将在任一侧显示 UIScrollView 中包含的部分视图。

这是一张图片;

UIScrollview 示例

图片显示了一个灰色区域,它是父视图或主视图。紫色区域是 UIScrollView 。白色区域是包含盒子图像、就绪标题和加急按钮的视图。

我遇到的问题是这个白色区域占据了整个空间,屏幕上没有视觉指示你甚至可以滚动,更不用说有更多的内容了。

理想情况下,我所追求的是 UIScrollView 内会有多个白色子视图,以便您可以在任一侧看到每个子视图的一部分,有点像这样:

概念

在这个概念中你会看到有一部分子视图被截断,但最重要的是你会在 UIScrollView 中看到多个子视图。

子视图(包含在白色部分中的那个)是 75(宽)x 160(高)。

然而,我似乎无法让它发挥作用。我尝试将 clipToBounds 设置为 yes 和 no,并且尝试强制设置内容大小;两者都给出了意想不到的结果。

如何让它在 UIScrollView 上显示超过 1 个 UIView,或在 UIScrollView 上显示部分 UIView?

非常感谢。

这是我的代码;

// Code follows
- (void)viewDidLoad
{
    // Scrollview
    int recordsFound = 6; // 6 products
    float scrollViewWidth = (self.scrollView.frame.size.width * recordsFound);
    float scrollViewHeight = (self.scrollView.frame.size.height);

    [self.scrollView setContentSize:CGSizeMake(scrollViewWidth, scrollViewHeight)];    
    [self.scrollView setClipsToBounds:YES];
    [self.scrollView setScrollEnabled:YES];
    [self.scrollView setPagingEnabled:YES];
    [self.scrollView setDirectionalLockEnabled:YES];
    [self.scrollView setUserInteractionEnabled:YES];
    [self.scrollView setShowsHorizontalScrollIndicator:YES];
    [self.scrollView setShowsVerticalScrollIndicator:NO];
    [self.scrollView setScrollEnabled:YES];
    [self.scrollView setDelegate:self];


    // view controllers are created lazily
    // in the meantime, load the array with placeholders which will be replaced on demand
    NSMutableArray *controllers = [[NSMutableArray alloc] init];
    for (unsigned i = 0; i < recordsFound; i++)
    {
        [controllers addObject:[NSNull null]];


    } // next

    //self.scrollView.bounds = CGRectMake(0, 0, 100, scrollView.frame.size.height);


    self.viewControllers = controllers;
    [controllers release];


    [self loadScrollViewWithPage:0];
    [self loadScrollViewWithPage:1];
}



-(void)loadScrollViewWithPage:(int)page
{
    int numberOfRecords = 6;

    if (page < 0)
        return;
    if (page >= numberOfRecords)
        return;

    // replace the placeholder if necessary
    SecondPageVC *controller = [viewControllers objectAtIndex:page];
    if ((NSNull *)controller == [NSNull null])
    {
        controller = [[SecondPageVC alloc] initWithPageNumber:page];        
        [viewControllers replaceObjectAtIndex:page withObject:controller];
        [controller release];
    }


    // add the controller's view to the scroll view
    if (controller.view.superview == nil)
    {
        CGRect frame    = scrollView.frame;
        frame.origin.x  = frame.size.width * page;
        frame.origin.y  = 0;
        controller.view.frame = frame;
        [scrollView addSubview:controller.view];

    }


}

-(void)scrollViewDidScroll:(UIScrollView *)sender
{
    // We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
    // which a scroll event generated from the user hitting the page control triggers updates from
    // the delegate method. We use a boolean to disable the delegate logic when the page control is used.
    if (pageControlBeingUsed)
    {
        // do nothing - the scroll was initiated from the page control, not the user dragging
        return;
    }

    // Switch the indicator when more than 50% of the previous/next page is visible
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];

    // A possible optimization would be to unload the views+controllers which are no longer visible
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 
{

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{

}
4

1 回答 1

0

我想我现在有一个解决方案,它并不完美,需要为您在图中看到的部分视图进行自定义。

我已经让它为完整视图工作。

我这样做的方法是创建一个大约 210w x 280h 的视图并将其设置在屏幕中央,下面的代码允许您在每一侧看到每个视图的一些(不同颜色)。

我在 Github 上找到了“ScrollViewPagingExample”(链接

// Card dimensions = 210 x 280

    // Scrollview    
    [_scrollView setClipsToBounds:NO];
    [_scrollView setPagingEnabled:YES];
    [_scrollView setShowsHorizontalScrollIndicator:NO];
    [_scrollView setAlwaysBounceHorizontal:YES];

    CGFloat contentOffset = 0.0f;


    for (int i = 0; i <5; i++ )
    {
        CGRect someFrame = CGRectMake(contentOffset, 0.0f, _scrollView.frame.size.width, _scrollView.frame.size.height);

        // Create view
        UIView *singleView = [[UIView alloc] initWithFrame:someFrame];

        switch (i) {
            case 0:
                [singleView setBackgroundColor:[UIColor greenColor]];
                break;
            case 1:
                [singleView setBackgroundColor:[UIColor darkGrayColor]];
                break;
            case 2:
                [singleView setBackgroundColor:[UIColor grayColor]];
                break;
            case 3:
                [singleView setBackgroundColor:[UIColor blueColor]];
                break;
            case 4:
                [singleView setBackgroundColor:[UIColor purpleColor]];
                break;
            default:
                //[singleView setBackgroundColor:[UIColor cyanColor]];
                break;
        }


        [singleView setContentMode:UIViewContentModeCenter];

        // Label
        UILabel *lblCardView = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
        [lblCardView setText:[NSString stringWithFormat:@"Card %d", i]]; 
        [lblCardView setTextColor:[UIColor whiteColor]];
        [lblCardView setBackgroundColor:[UIColor clearColor]];

        [singleView addSubview:lblCardView];

        // Add to scrollview        
        [_scrollView addSubview:singleView];

        // Memory management
        [singleView release];
        [lblCardView release];


        // Update contentOffset with padding
        contentOffset += singleView.frame.size.width +35;

        _scrollView.contentSize = CGSizeMake(contentOffset, _scrollView.frame.size.height);

    } // next
于 2011-09-04T16:35:02.160 回答