1

を保持するUITableViewがあります。ここで、 に aを追加したいと思い ます。しかし、これはうまくいきません。self.viewの作品...UITableViewCellUIImageViewUILongGestureRecognizerUIImageViewUILongGestureRecognizer

UILongGestureRecognizerで動作する を実装する方法UIImageViewUITableViewCell?

TableViewController.h

@interface MagTableViewController : UITableViewController <UIGestureRecognizerDelegate>

@property (strong, nonatomic) UILongPressGestureRecognizer *longPress;
@property (strong, nonatomic) NSMutableArray *tableContent;

@end

TableViewController.m

- (void)viewDidLoad
{
    self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
    self.longPress.minimumPressDuration = 0.2;
    self.longPress.numberOfTouchesRequired = 1;
    //[self.view addGestureRecognizer:self.longPress];  // This works! 
}

// [...]

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UIImageView *imvLeft = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    [imvLeft setImageWithURL:[NSURL URLWithString:self.tableContent[@"url"]]];
    imvLeft.userInteractionEnabled = YES; // added soryngod's hint, but does not
    // solve the problem, as only the last row of 5 is enabled...
    [imvLeft addGestureRecognizer:self.longPress];  // does not work... 

    [cell.contentView addSubview:imvLeft];

    return cell;
}

-(void)longPressed:(UILongPressGestureRecognizer *)recognizer {
// do stuff

}
4

2 に答える 2

1

imvLeft.userInteractionEnabled = YES; デフォルトで設定する必要がありますNO

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
        longPress.minimumPressDuration = 0.2;
        longPress.numberOfTouchesRequired = 1;
        imvLeft.userInteractionEnabled = YES;
        [imvLeft addGestureRecognizer:self.longPress];
        [longPress release];
    [cell.contentView addSubview:imvLeft];

    return cell;
}

そして、押されたイメージビューを特定したい場合

-(void)longPressed:(UILongPressGestureRecognizer *)recognizer 
{

 UIImageView *img = (UIImageView *)recognizer.view;

//do stuff
}
于 2013-07-16T14:05:16.457 に答える
1

の設定に加えてimvLeft.userInteractionEnabled = YES、画像ビューごとに個別のジェスチャ認識エンジンを作成する必要もあります。設計上、UIGestureRecognizer単一のビューに関連付ける必要があります。表示されている症状は、新しいセルがaddGestureRecognizer:.

関連する質問を参照してください: UIGestureRecognizer を複数のビューにアタッチできますか?

于 2013-07-16T19:05:48.017 に答える