本文介绍了将参数传递给UITapGestureRecognizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了动态 UIimage
视图和 UITapGestureRecognizer
到视图中它看起来像这样
i have created dynamic UIimage
view and and UITapGestureRecognizer
to the view its look like this
UIImageView *image = [[UIImageView alloc ] initWithFrame:CGRectMake(x, 0, 200, 150)];
NSString *ImageURL = [str objectForKey:@"imageLink"];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
image.image = [UIImage imageWithData:imageData];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
singleTap.numberOfTapsRequired = 1;
image.userInteractionEnabled = YES;
[image addGestureRecognizer:singleTap];
[documentory addSubview:image];
和我这样的行动方法
-(void)tapDetected:(UIGestureRecognizer *)recognizer{
NSLog(@"single Tap on imageview");
}
我想将一个字符串参数传递给tapDetected方法可以任何人告诉我如何做到这一点非常感谢
i want to pass a string parameters to tapDetected method can anyone tell me how to do this thank you very much
推荐答案
您可以扩展UITapGestureRecognizer来保存更多数据:
You can extend a UITapGestureRecognizer for holding more data:
// MYTapGestureRecognizer.h
@interface MYTapGestureRecognizer : UITapGestureRecognizer
@property (nonatomic, strong) NSString *data;
@end
// MYTapGestureRecognizer.m
@implementation MYTapGestureRecognizer
@end
// =====================
....
MYTapGestureRecognizer *singleTap = [[MYTapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
singleTap.data = @"Hello";
.....
// ====================
-(void)tapDetected:(UITapGestureRecognizer *)tapRecognizer {
MYTapGestureRecognizer *tap = (MYTapGestureRecognizer *)tapRecognizer;
NSLog(@"data : %@", tap.data);
}
这篇关于将参数传递给UITapGestureRecognizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!