简体   繁体   English

UIGestureRecognizer在UIImageView上无法正常工作

[英]UIGestureRecognizer not working properly on UIImageView

Introduction:- 介绍:-
In my project, i'm using UITapGestureRecognizer on UIImageView contained in Custom TableViewCell( videoTableViewCell.h ). 在我的项目中,我在Custom TableViewCell( videoTableViewCell.h )中包含的UIImageView上使用UITapGestureRecognizer。 i added a tapGesture & longPressGesture to perform some actions. 我添加了tapGesture和longPressGesture来执行一些操作。 after seeing this Question i enabled userInteraction.than i got response from cell.image2_2 and cell.image1_1 . 看到这个问题后,我启用了userInteraction.than,我收到了cell.image2_2cell.image1_1响应。
Issue:- but cell.image1_2 do not respond on tapGesture or longPressGesture. 问题:-但是cell.image1_2在tapGesture或cell.image1_2上没有响应。
note: image1_2 and image2_2 is in one cell(Reuse Identifier two ) and image1_1 is in other cell(Reuse Identifier one ) 注意: image1_2和image2_2在一个单元格(重用标识符two )中,而image1_1在另一个单元格(重用标识符one )中
Macros 巨集

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

videoViewController.m videoViewController.m
-->UITableView DataSource Method -> 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 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;
    }
}



if anyone need more information, just ask me. 如果有人需要更多信息,请问我。
by the way, thanks for paying attention. 顺便说一句,感谢您的关注。

So finally i got the point. 所以最后我明白了。
The problem is i'm creating one UITapGestureRecognizer and UILongPressGestureRecognizer object and uses it on more than one object. 问题是我正在创建一个UITapGestureRecognizerUILongPressGestureRecognizer对象,并在多个对象上使用它。

So, here is solution.. 所以,这是解决方案。
Use new UIGestureRecognizer for every object like bellow: 对下面的每个对象使用新的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM