2

「ページ」ごとに1つの画像を含む、複数の画像を含むページングでUIScrollViewを作成しましたが、画像を互いに区別する方法があるかどうか疑問に思っていましたか? Fx。最初の画像が表示されているときは、UILabel を「1」に設定するボタンがあり、2 番目の画像が表示されているときは、同じボタンが同じ UILabel を「2」に設定します。

これが私のコードです:

pictures = [NSArray arrayWithObjects: [UIImage imageNamed:@"AND.png"], [UIImage imageNamed:@"ALB.png"], nil];

    for (int i = 0; i < pictures.count; i++) {
        CGRect frame;
        frame.origin.x = 135 + (self.scrollViewQuiz.frame.size.width * i);
        frame.origin.y = 40;
        frame.size = CGSizeMake(50, 50);

        UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
        subview.image = [pictures objectAtIndex:i];
        [self.scrollViewQuiz addSubview:subview];
    }

    self.scrollViewQuiz.contentSize = CGSizeMake(self.scrollViewQuiz.frame.size.width * pictures.count, self.scrollViewQuiz.frame.size.height);
4

3 に答える 3

1

スクロールビューが現在表示している「ページ番号」を計算したいだけのようです。ボタンのアクションとして、次のようなものを試すことができます。

- (IBAction)buttonWasTapped:(id)sender {
    UIScrollView *scrollView = self.scrollViewQuiz;
    int pageNumber = (int)roundf(scrollView.contentOffset.x / scrollView.bounds.size.width);
    self.label.text = [NSString stringWithFormat:@"%d", pageNumber];
}
于 2012-06-16T20:49:23.837 に答える
0

次の scrollview デリゲート メソッドを実装できます。

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

その中で、スクロールビューの contentOffset を調べ、それに応じてラベルを設定します。

于 2012-06-16T20:43:22.763 に答える
0

これら2つを試してください..これらはうまくいくはずです..これが役立つことを願っています..

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
               CGFloat xOffset = scrollView.contentOffset.x;
   int currentPage = floor(xOffset / scrollView.bounds.size.width);
    [itsLabel setText:[NSString stringWithFormat:@"%i", currentPage]];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
               CGFloat xOffset = scrollView.contentOffset.x;
   int currentPage = floor(xOffset / scrollView.bounds.size.width);
    [itsLabel setText:[NSString stringWithFormat:@"%i", currentPage]];
}
于 2012-06-16T20:46:28.253 に答える