5

10ページあるとしましょう。

イベントのイベント ハンドラーが次のように追加されます。

アプリを実行します。これで 10 ページになりましたが、デフォルトではページ 1 (インデックス 0) が選択されています。2 ページ目または 3 ページ目をタッチします。イベントはトリガーされません。最後のページが選択されると、イベントがトリガーされます。最後のページでも同じことが起こります。最後のページを選択したら、前のページを選択します。イベントは発生しませんが、最初のページを選択するとイベントは発生しません。

このケースの簡単なデモを見るには、UICatalog サンプルをダウンロードして ControlsViewController.m を開き、375 行目で UIControlEventTouchUpInside を UIControlEventValueChanged に変更します。

- (UIPageControl *)pageControl
{
    if (pageControl == nil) 
    {
        CGRect frame = CGRectMake(120.0, 14.0, 178.0, 20.0);
        pageControl = [[UIPageControl alloc] initWithFrame:frame];
        [pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];

        // in case the parent view draws with a custom color or gradient, use a transparent color
        pageControl.backgroundColor = [UIColor grayColor];

        pageControl.numberOfPages = 10; // must be set or control won't draw
        pageControl.currentPage = 0;
        pageControl.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
    }
    return pageControl;
}
4

2 に答える 2

10

ページ コントロールの仕組みを誤解している可能性があります。これは現在のページ数を視覚的に示していますが、特定のドットをタップしてもそのページには移動しません。一度に 1 ページだけ移動します。左半分をタップするとページが戻り、右半分をタップするとページが進みます。

最初のページで左半分をタップすると、別のページに戻ることはできないため、何も起こりません。

特に iPad での動作はあまり好きではないので、通常はサブクラスまたは独自のタッチ処理を使用して、タッチ位置が現在選択されているページの左または右にあるかどうかを判断し、イベントを適切に送信します。

于 2012-11-22T07:53:41.717 に答える
0

addTarget:self action:@selector()を使用する代わりに、UITapGestureRecognizerを使用できます。

//Added UITapGestureRecognizer instead of using addTarget: method        
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init];         
tapGesture addTarget:self action:@selector(pageAction:) ];
[pageControl addGestureRecognizer:tapGesture];
[tapGesture release];
tapGesture = nil;


-(void)pageAction:(UITapGestureRecognizer *)tapGesture{  

   UIPageControl *pageControl = (UIPageControl *)tapGesture.view;      

   NSLog(@"page Number: %d",pageControl.currentPage);


}
于 2012-11-22T08:16:57.737 に答える