0

3 つの画像間を水平方向にスクロールするページングを使用して、単純な UIScrollView を構築しようとしています。トリッキーな部分は、各画像をクリック可能にしてクリックイベントをキャッチしたいということです。

私のテクニックは、それぞれが UIImage で構成される 3 つの UIButton を作成することです。各ボタンにタグを付けてアクションを設定します。

問題: クリック イベントをキャッチできますが、スクロールできません。

これが私のコードです:

- (void) viewDidAppear:(BOOL)animated {

    _imageArray = [[NSArray alloc] initWithObjects:@"content_01.png", @"content_02.png", @"content_03.png", nil];

    for (int i = 0; i < [_imageArray count]; i++) {
        //We'll create an imageView object in every 'page' of our scrollView.
        CGRect frame;
        frame.origin.x = _contentScrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = _contentScrollView.frame.size;

        //
        //get the image to use, however you want
        UIImage* image = [UIImage imageNamed:[_imageArray objectAtIndex:i]];

        UIButton* button = [[UIButton alloc] initWithFrame:frame];

        //set the button states you want the image to show up for
        [button setImage:image forState:UIControlStateNormal];
        [button setImage:image forState:UIControlStateHighlighted];

        //create the touch event target, i am calling the 'productImagePressed' method
        [button addTarget:self action:@selector(imagePressed:)
         forControlEvents:UIControlEventTouchUpInside];
        //i set the tag to the image #, i was looking though an array of them
        button.tag = i;

        [_contentScrollView addSubview:button];
    }

    //Set the content size of our scrollview according to the total width of our imageView objects.
    _contentScrollView.contentSize = CGSizeMake(_contentScrollView.frame.size.width * [_imageArray count], _contentScrollView.frame.size.height);

    _contentScrollView.backgroundColor = [ENGAppDelegate backgroundColor];
    _contentScrollView.delegate = self;
}
4

1 に答える 1

1

UIButtonはサブクラスであるためUIControl、スクロール ビューのタッチを「食い尽くします」:

[UIScrollView touchesShouldCancelInContentView:]view が UIControl オブジェクトでない場合、デフォルトの戻り値は YES です。それ以外の場合は、NO を返します。

( https://developer.apple.com/library/ios/documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html#//apple_ref/occ/instm/UIScrollView/touchesShouldCancelInContentViewから:)

サブクラス化と上書き(および/または)によってこれに影響を与えることができます。ただし、あなたのユースケースでは、そもそもボタンを使用しません。スクロールビューにタップジェスチャ認識機能を追加し、タッチポイントを使用してどの画像がタップされたかを判断してみませんか? これははるかに簡単で、問題なく動作するはずです。UIScrollViewtouchesShouldCancelInContentView:touchesShouldBegin:withEvent:inContentView:

于 2013-10-06T21:20:09.283 に答える