0

私はそれについて多くの人々が尋ねているのを見ました、私はすべてを読みましたが、それでもそれを行う方法を理解することができませんでした。ページングが有効になっている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;
}

このコードは、私がこれに適合させようとしている別のアプリからの何かの組み合わせですが、成功していません。助けてください。どうもありがとう。

4

1 に答える 1

1

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;
}
于 2012-08-05T13:53:50.107 に答える