ios – 将参数传递给UITapGestureRecognizer [复制]

前端之家收集整理的这篇文章主要介绍了ios – 将参数传递给UITapGestureRecognizer [复制]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Pass a NSDictionary as parameter to UITapGestureRecognizer3个
我已经为视图创建了动态UIimage视图和UITapGestureRecognizer,它看起来像这样
UIImageView *image = [[UIImageView alloc ] initWithFrame:CGRectMake(x,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方法可以有人告诉我如何做到这一点非常感谢你

解决方法

您可以扩展UITapGestureRecognizer以保存更多数据:
// 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);

}
原文链接:https://www.f2er.com/iOS/333259.html

猜你在找的iOS相关文章