UIGestureRecognizer 在 UIImageView 上无法正常工作
UIGestureRecognizer not working properly on UIImageView
简介:-
在我的项目中,我在自定义 TableViewCell(videoTableViewCell.h
) 中包含的 UIImageView 上使用 UITapGestureRecognizer。我添加了一个 tapGesture 和 longPressGesture 来执行一些操作。看到这个 Question
我启用了 userInteraction.than 我得到了 cell.image2_2
和 cell.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;
}
}
如果有人需要更多信息,请问我。
顺便感谢关注
所以我终于明白了。
问题是我正在创建一个 UITapGestureRecognizer
和 UILongPressGestureRecognizer
对象并将其用于多个对象。
所以,这是解决方案..
对每个对象使用新的 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
简介:-
在我的项目中,我在自定义 TableViewCell(videoTableViewCell.h
) 中包含的 UIImageView 上使用 UITapGestureRecognizer。我添加了一个 tapGesture 和 longPressGesture 来执行一些操作。看到这个 Question
我启用了 userInteraction.than 我得到了 cell.image2_2
和 cell.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;
}
}
如果有人需要更多信息,请问我。
顺便感谢关注
所以我终于明白了。
问题是我正在创建一个 UITapGestureRecognizer
和 UILongPressGestureRecognizer
对象并将其用于多个对象。
所以,这是解决方案..
对每个对象使用新的 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