1

このサイトでこのソリューションを入手しました。UIImageをクリックして、Objective-cでUIImageViewを開きます。

UITapGestureRecognizerあなたに追加UIImageView

UITapGestureRecognizer *tapRecognizer;
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourSelector)];
[thumbnail addGestureRecognizer:tapRecognizer];
[tapRecognizer release];

thumbnail.userInteractionEnabled = YES; // very important for UIImageView

これは単一のImageViewで非常にうまく機能していますが、scrollViewに複数(約20)を追加しています。次に、ユーザーがタップまたは選択するImageViewを区別するにはどうすればよいですか。独自の@selector(imageClicked)を設定しようとしましたが、最後のimageViewのタグしか返されません。

imageViewに20枚の静止画像を動的に読み込むため、addGestureRecognizerをループに追加しています。

4

5 に答える 5

5

これは役立つかもしれません

for(int i=0;i<20;i++)
{
    UIImageView *img=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yourimage.png"]];
    [img setTag:i];
    img.frame= //set frame accordingly;
    img.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [img addGestureRecognizer:tap];
    [tap release];
    [scrollView addSubView:img];
}

- (void)handleTap:(UITapGestureRecognizer *)recognizer  {
    UIImageView *imageView = (UIImageView *)recognizer.view;

      switch([imageView tag])
    {
       case 1:
          //do your work
          break;
       .
       .
       .
       .
       case n:

    }
}
于 2012-01-31T07:48:48.523 に答える
1
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
for (UIImageView *thumbnail in imageArray)  {
    [thumbnail addGestureRecognizer:tapRecognizer];
}
[tapRecognizer release];

ビューはUIGestureRecognizerのプロパティ「view」から取得できます。たとえば、セレクターで:

- (void)handleTap:(UITapGestureRecognizer *)recognizer  {
    UIImageView *imageView = (UIImageView *)recognizer.view;

    //  Now do something with your view
}
于 2012-01-31T07:16:58.003 に答える
1

シングル タップ レコグナイザーを複数のビューに追加することはできません。タップ認識エンジンを追加するすべてのビューに対して新しいものを作成します。テーブルビューを使用しているので、メソッドでそれを行うだけですtableView:cellForRowAtIndexPath::

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // usual implementation
    static NSString *cellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeue ...];
    if (!cell) {
        cell = [[UITableViewCell alloc] init....];
        // add new gesture recognizer here.
    }

    // setup cell: set the image (just an example)
    cell.imageView.image = [images objectAtIndex:indexPath.row];

    return cell;
}

他の回答で言及されているようなタグを使用する代わりに、イメージビューを取得するだけで、基礎となるモデルを操作しようとします。タップを処理するときは、どのモデル オブジェクトにアクセスするかを知るために indexPath を見つけます。

- (void)handleTap:(UITapGestureRecognizer *)recognizer  {
    UIImageView *imageView = (UIImageView *)recognizer.view;

    // assumes the image view is direct subview of the cell
    // change to match your cell structure
    UITableViewCell *cell = (UITableViewCell *) [imageView superview]; 

    // get the index path for the cell clicked
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    //  TODO: Use index path to get full image to display
}

このようにして、クリックされた画像の正確な行がわかるため、モデルにアクセスして表示する画像全体にアクセスできます。

于 2012-01-31T08:06:54.840 に答える
0

ImageView をサブクラス化して、サブクラスにジェスチャ認識機能を追加してください。

次に、画像ごとに ImageView オブジェクトを作成し、そのオブジェクトに画像を追加します。画像の名前のように、どのオブジェクトをクリックするかを識別できるように、いくつかの一意のプロパティを設定します。

于 2012-01-31T08:59:42.027 に答える
0

すべての画像にタグを追加する必要があります-たとえば、thumbnail.tag = 100.次に、セレクターを youSelector:(UITapGestureRecognizer *)sender; に変更します。セレクターにスイッチを追加

- (void) yourSelector:(UITapGestureRecognizer *)sender {
    UIImageView *imageView = (UIImageView *)sender.view;
    switch(imageView.tag) {
        case 100: {
        //This code will be handled if tag == 100;
        }
    }
}
于 2012-01-31T07:23:33.370 に答える