UITapGestureRecognizer 不适用于 UIImageView 自定义类

Posted

技术标签:

【中文标题】UITapGestureRecognizer 不适用于 UIImageView 自定义类【英文标题】:UITapGestureRecognizer not working with UIImageView Custom class 【发布时间】:2016-10-14 14:38:29 【问题描述】:

UITapGestureRecognizer 无法在我的 UIViewController 上使用我的 UIImageView 类。

HousePictureView *pictureHouse = [[HousePictureView alloc] init:[dictionary objectForKey:@"photo_url"]];
UITapGestureRecognizer *picturestap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showpictures)];
picturestap.numberOfTapsRequired = 1;
[pictureHouse addGestureRecognizer:picturestap];
[parentScroll addSubview:pictureHouse];

HousePictureView.m

@interface HousePictureView() 
    CGFloat screenwidth;


@property (strong, nonatomic) UIImageView *pictureView;

@end

@implementation HousePictureView


-(id)init:(NSString*)url

    screenwidth = [UIScreen mainScreen].bounds.size.width; // Width of this screen
    if(self = [super init])
    
        _pictureView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,screenwidth,300)];
        [_pictureView setImageWithURL:[NSURL URLWithString:url] placeholderImage:[Functions resizeImage:[UIImage imageNamed:@"nophoto"] newSize:CGSizeMake(screenwidth,300)]];
        _pictureView.userInteractionEnabled = YES;
        [self addSubview:_pictureView];
    
    return self;


@end

HousePictureView.h

@interface HousePictureView : UIImageView

- (id)init:(NSString*)url;

@end

为什么在 userInteractionEnabled 表单视图父级被激活时不可能有事件?

感谢您的帮助:)

【问题讨论】:

init 中缺少self.userInteractionEnabled = YES; @Larme userInteractionEnabled 默认为 YES。 不在UIImageView 上,或者那是新的。 @Larme 是正确的:在UIImageView reference 的响应触摸事件下,它指定userInteractionEnabled 默认设置为忽略用户交互。跨度> 为什么? userInteractionEnabled 在此函数上设置为 YES。 【参考方案1】:

mmmhhh 你已经创建了一个 UIImageView 自定义类,其中添加了一个属性 UIImageView,就像子视图一样。为什么? 只需使用“自我”。

它不起作用,因为您要在另一个 UIImageView 下的 UIImageView 上添加点击手势。

【讨论】:

但是只是使用“self”是行不通的。我不明白,没有 UIImageView 正在工作。 :/ 在自定义类中使用 touch 怎么办? 使用 self 不起作用?为什么?在那种情况下你做了什么?

以上是关于UITapGestureRecognizer 不适用于 UIImageView 自定义类的主要内容,如果未能解决你的问题,请参考以下文章