【问题标题】:dynamic UIScrollView with paging and data from NSMutableArray带有 NSMutableArray 的分页和数据的动态 UIScrollView
【发布时间】:2012-08-05 12:35:27
【问题描述】:

我看到很多人问这个问题,我阅读了所有内容,但仍然不知道该怎么做。我有一个启用分页的 UIScrollView,还有一个带字符串的 NSMutableArray。

我想要做的是 UIScrollView 将与字符串一起显示给 UITextView,当我切换到下一页时,它将显示 NSMutableArray 上的下一项。

由于某种原因,我无法让它工作。

- (void)setupPage
{
    pagingView.delegate = self;

    [self.pagingView setBackgroundColor:[UIColor blackColor]];
    [pagingView setCanCancelContentTouches:NO];

    pagingView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    pagingView.clipsToBounds = YES;
    pagingView.scrollEnabled = YES;
    pagingView.pagingEnabled = YES;
    pagingView.backgroundColor = [UIColor clearColor];
    [pagingView setShowsHorizontalScrollIndicator:NO];
    [pagingView setShowsVerticalScrollIndicator:NO];

    for (int i=0;i<[arrCat count];i++)
    {
        UITextView * txtJoke = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
        [pagingView addSubview:txtJoke];

        txtJoke.text = [arrCat objectAtIndex:i];
        txtJoke.textAlignment = UITextAlignmentRight;
    }
    [pagingView setContentSize:CGSizeMake(960, [pagingView bounds].size.height)];
}

- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
    if (pageControlIsChangingPage) {
        return;
    }

    /*
     *  We switch page at 50% across
     */
    CGFloat pageWidth = _scrollView.frame.size.width;
    int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    //pageControl.currentPage = page;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
    pageControlIsChangingPage = NO;
}

- (IBAction)changePage:(id)sender :(int)current
{
    /*
     *  Change the scroll view
     */
    CGRect frame = pagingView.frame;
    frame.origin.x = frame.size.width * 1;
    frame.origin.y = 0;

    [pagingView scrollRectToVisible:frame animated:YES];

    /*
     *  When the animated scrolling finishings, scrollViewDidEndDecelerating will turn this off
     */
    pageControlIsChangingPage = YES;
}

此代码是我试图融入其中但没有任何成功的另一个应用程序中的某些内容的组合。 请帮忙。非常感谢。

【问题讨论】:

    标签: iphone objective-c ios xcode uiscrollview


    【解决方案1】:

    一方面,您所有的 UITextView 都具有相同的框架坐标,因此它们将相互重叠。假设您正在水平滚动,您需要将 UITextView 的 x 原点偏移页面宽度。

    您使用的是 UIPageControl 吗?如果没有,您可以删除 setupPage 以外的方法。那些其他方法只是用于更改 UIPageControl,以及使用 UIPageControl 更改页面。

    这是一个与您类似的基本滚动视图实现:

    static NSUInteger kNumberOfPages = 3;
    static NSUInteger kPageWidth = 320;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.scrollView.pagingEnabled = YES;
        self.scrollView.contentSize = CGSizeMake(kPageWidth * kNumberOfPages, self.scrollView.frame.size.height);
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.delegate = self;
    
        self.pageOne.frame = CGRectMake(0, 0, kPageWidth, self.pageOne.frame.size.height);
        [self.scrollView addSubview:self.pageOne];
    
        self.pageTwo.frame = CGRectMake(kPageWidth, 0, kPageWidth, self.pageTwo.frame.size.height);
        [self.scrollView addSubview:self.pageTwo];
    
        self.pageThree.frame = CGRectMake(2*kPageWidth, 0, kPageWidth, self.pageThree.frame.size.height);
        [self.scrollView addSubview:self.pageThree];
    
        self.pageControl.numberOfPages = kNumberOfPages;
        self.pageControl.currentPage = 0;
    }
    
    # pragma mark - Scroll View Related Methods
    
    - (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 (pageControlUsed_)
        {
            // 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
        int page = floor((self.scrollView.contentOffset.x - kPageWidth / 2) / kPageWidth) + 1;
        self.pageControl.currentPage = page;
    }
    
    // At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
        pageControlUsed_ = NO;
    } 
    
    // At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        pageControlUsed_ = NO; 
    }
    
    - (IBAction)changePage:(id)sender
    { 
        int page = self.pageControl.currentPage;
    
    // update the scroll view to the appropriate page
        CGRect frame = self.scrollView.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        [self.scrollView scrollRectToVisible:frame animated:YES];
    
    // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
        pageControlUsed_ = YES;
    }
    

    【讨论】:

    • 这很容易,只需偏移 UITextViews 的帧就可以了。
    • 是否可以在没有 PageControl 的情况下使用分页?对吧?
    • 是的,您不需要页面控件来在滚动视图中进行分页。另外我注意到你没有明确设置滚动视图的框架,大概宽度是 320?
    • 我不想像这样定义页面,我有 NSMutableArray 可以说 10 个对象,我想将每个项目内的文本加载到 UIScrollView 中的每个页面,我希望它是动态的.
    • 如果你有那么多,最好只添加你需要的视图:你当前正在查看的页面上的一个,之前页面上的一个,页面上的一个后。滚动时,使用 - (void)scrollViewDidScroll:(UIScrollView *)sender 检查页面是否发生变化,并在滚动视图中添加和删除视图。
    猜你喜欢
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多