微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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);

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐