【问题标题】:Detecting touch on SVGKit's SVGKImageView subclass检测 SVGKit 的 SVGKImageView 子类的触摸
【发布时间】:2015-08-24 08:45:34
【问题描述】:

我已经开始在 iOS 上使用这个很棒的 SVGKit。我正在使用SVGKImageView 的子类。现在,在子类化SVGKImageView 之前,我可以很容易地向它添加UITapGestureRecognizer。但是我的要求使我必须使用子类,因为我必须将数十个 SVGKImageViewon 放在我的父视图中。

(这是因为在其他视图上会有SVGKImageViewtransparent 部分,我希望能够忽略 alpha = 0 的视图区域上的触摸,所以我希望能够检测单个的触摸SVGKImageView 然后检查 alpha 值,如果触摸在透明区域,则将触摸事件转发到下一个视图,依此类推,直到找到某个视图的非透明区域

现在子类化 SVGKImageViewadding UITapGestureRecognizer 不起作用,这也是

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

在我的子类中没有调用。我还将 SVGKImageView 的 userInteractionEnabled 设置为 YES 但无济于事。

谁能帮助告诉我为什么没有触摸/点击事件传递给我的 SVGKImageView 子类?

下面是我的子类init方法

- (instancetype)init{

    self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(svgImageTapped:)];
    self.tapGestureRecognizer.numberOfTapsRequired = 1;
    self.tapGestureRecognizer.delegate = self;
    [self addGestureRecognizer:self.tapGestureRecognizer];

    self.imageSVG = [SVGKImage imageNamed:@"SomeFile.svg"];
    self = (Subclass*)[[SVGKLayeredImageView alloc] initWithSVGKImage:self.imageSVG];
    [self sizeToFit];
    self.userInteractionEnabled = YES;

    self = [super init];

    return self;
}

【问题讨论】:

    标签: ios objective-c svgkit


    【解决方案1】:

    让您订购代码:

    - (instancetype)init{
            self = [super init]; // <--- should go first
    
            self = (Subclass*)[[SVGKLayeredImageView alloc] initWithSVGKImage:[SVGKImage imageNamed:@"SomeFile.svg"]];
    
            // self.imageSVG = [SVGKImage imageNamed:@"SomeFile.svg"];
            [self sizeToFit];
            self.userInteractionEnabled = YES;
    
            return self;
    }
    

    【讨论】:

    • 我在有和没有UITapGestureRecognizer 的情况下进行了测试,但无济于事。也 [tapGestureRecognizer setCancelsTouchesInView:NO];没用的
    • 手势有回调吗?
    • alloc init 使用 nil 图像指针会导致崩溃,你不觉得吗?
    • 抱歉,再次编辑。我认为问题是 self = [super init]; ,你应该先调用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多