UIGestureRecognizer在UIImageView上无法正常工作

时间:2015-09-29 07:36:58

标签: ios objective-c iphone uitableview uiimageview

简介: -
在我的项目中,我在Custom TableViewCell(videoTableViewCell.h)中包含的UIImageView上使用UITapGestureRecognizer。我添加了tapGesture& longPressGesture执行一些操作。看到这个Question后 我启用了userInteraction.than我得到了cell.image2_2cell.image1_1的回复 问题:,但cell.image1_2没有回复tapGesture或longPressGesture。
注意: image1_2和image2_2位于一个单元格中(重用标识符two),而image1_1位于其他单元格中(重用标识符one

  RADIOUS 16.0
  TITLE_COLOR [UIColor colorWithRed:102.0/255.0 green:255.0/255.0 blue:0/255.0 alpha:1]

videoViewController.m
- > UITableView数据源方法

-(void)tableView:(UITableView *)tableView willDisplayCell:(videoTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Action:)];
    tapGesture.numberOfTapsRequired = 1;
    UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
    if(indexPath.row%2 == 0){//cell with two boxes
        NSArray* array = [newArray objectAtIndex:indexPath.row];
        if(array.count > 1){//WHEN CONATAINS TWO OBJECTS in Array
            NSString* string = [array objectAtIndex:0];
            if(string){
                cell.image1_2.hidden = NO;
            }else{
                cell.image1_2.hidden = YES;
            }
            [cell.image1_2 setImage:[self getThumbNailByName:string]];
            NSString*tag = [NSString stringWithFormat:@"%ld%ld",indexPath.row,(long)0];
            cell.image1_2.tag = [tag integerValue];
            [cell.image1_2 addGestureRecognizer:tapGesture];
            if(selectionEnabled){
                if([selectedItems containsObject:[NSNumber numberWithInteger:[tag integerValue]]]){
                    cell.image1_2.layer.borderWidth = 1.0;
                    cell.image1_2.layer.borderColor = TITLE_COLOR.CGColor;
                }else{
                    cell.image1_2.layer.borderColor = [UIColor clearColor].CGColor;
                    cell.image1_2.layer.borderWidth = 0;
                }
            }else{
                [cell.image1_2 addGestureRecognizer:longPress];
            }

            string = [array objectAtIndex:1];
            if(string){
                cell.image2_2.hidden = NO;
            }else{
                cell.image2_2.hidden = YES;
            }
            [cell.image2_2 setImage:[self getThumbNailByName:string]];
            tag = [NSString stringWithFormat:@"%ld%ld",indexPath.row,(long)1];
            cell.image2_2.tag = [tag integerValue];
            [cell.image2_2 addGestureRecognizer:tapGesture];
            if(selectionEnabled){
                if([selectedItems containsObject:[NSNumber numberWithInteger:[tag integerValue]]]){
                    cell.image2_2.layer.borderWidth = 1.0;
                    cell.image2_2.layer.borderColor = TITLE_COLOR.CGColor;
                }else{
                    cell.image2_2.layer.borderColor = [UIColor clearColor].CGColor;
                    cell.image2_2.layer.borderWidth = 0;
                }
            }else{
               [cell.image2_2 addGestureRecognizer:longPress];
            }
        }else{// WHEN CONATAINS ONLY ONE OBJECT in Array
            NSString* string = [array objectAtIndex:0];
            if(string){
                cell.image1_2.hidden = NO;
            }else{
                cell.image1_2.hidden = YES;
            }
            NSString*tag = [NSString stringWithFormat:@"%ld%ld",indexPath.row,(long)0];
            cell.image1_2.tag = [tag integerValue];
            [cell.image1_2 addGestureRecognizer:tapGesture];
            cell.image2_2.hidden = YES;
            if(selectionEnabled){
                if([selectedItems containsObject:[NSNumber numberWithInteger:[tag integerValue]]]){
                    cell.image1_2.layer.borderWidth = 1.0;
                    cell.image1_2.layer.borderColor = TITLE_COLOR.CGColor;
                }else{
                    cell.image1_2.layer.borderColor = [UIColor clearColor].CGColor;
                    cell.image1_2.layer.borderWidth = 0;
                }
            }else{
                [cell.image1_2 addGestureRecognizer:longPress];
            }
        }
    }else{//cell with one box
        NSString* string = [[newArray objectAtIndex:indexPath.row] objectAtIndex:0];
        if(string){
            cell.image1_1.hidden = NO;
        }else{
            cell.image1_1.hidden = YES;
        }
        [cell.image1_1 setImage:[self getThumbNailByName:string]];
        NSString*tag = [NSString stringWithFormat:@"%ld%ld",indexPath.row,(long)0];
        cell.image1_1.tag = [tag integerValue];
        [cell.image1_1 addGestureRecognizer:tapGesture];
        if(selectionEnabled){
            if([selectedItems containsObject:[NSNumber numberWithInteger:[tag integerValue]]]){
                cell.image1_1.layer.borderWidth = 1.0;
                cell.image1_1.layer.borderColor = TITLE_COLOR.CGColor;
            }else{
                cell.image1_1.layer.borderColor = [UIColor clearColor].CGColor;
                cell.image1_1.layer.borderWidth = 0;
            }
        }else{
            [cell.image1_1 addGestureRecognizer:longPress];
        }
    }
}


videoTableViewCell.m

- (void)awakeFromNib
{
    // Initialization code
    [image1_1 setUserInteractionEnabled:YES];
    [image1_2 setUserInteractionEnabled:YES];
    [image2_2 setUserInteractionEnabled:YES];
    if([self.reuseIdentifier isEqualToString:@"one"]){
        image1_1.layer.cornerRadius = RADIOUS;
        image1_1.clipsToBounds = YES;
    }else{
        //Two Album Cell'cell2'
        image1_2.layer.cornerRadius = RADIOUS;
        image1_2.clipsToBounds = YES;
        image2_2.layer.cornerRadius = RADIOUS;
        image2_2.clipsToBounds = YES;
    }
}



如果有人需要更多信息,请问我。顺便说一句,谢谢你的关注。

1 个答案:

答案 0 :(得分:0)

所以最后我明白了。
问题是我正在创建一个UITapGestureRecognizerUILongPressGestureRecognizer对象,并在多个对象上使用它。

所以,这是解决方案..
对下面的每个对象使用新的UIGestureRecognizer

UITapGestureRecognizer* tapGesture1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Action:)];
    tapGesture1.numberOfTapsRequired = 1; 
[cell.image1_1 addGestureRecognizer:tapGesture1];

UITapGestureRecognizer* tapGesture2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Action:)];
    tapGesture2.numberOfTapsRequired = 1; 
[cell.image1_2 addGestureRecognizer:tapGesture2];

UITapGestureRecognizer* tapGesture3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Action:)];
        tapGesture3.numberOfTapsRequired = 1; 
    [cell.image2_2 addGestureRecognizer:tapGesture3];
//do same for UILogPressGestureRecognizer