Detecting touch on SVGKit's SVGKImageView subclass

511 Views Asked by At

I have started using this great SVGKit for iOS. I am using a subclass of SVGKImageView. Now before subclassing SVGKImageView i have been easily able to add a UITapGestureRecognizer to it. But my requirement made me to use subclasses as i have to place tens of SVGKImageViewon to my parent view.

(This is because there would be SVGKImageViewtransparent portions on other views and i want to be able to ignore touches on the views areas where alpha = 0 so i want to be able to detect touch on a single SVGKImageView then check for alpha value and if touch is on transparent area forward the touch event to the next view and so on until non transparent area of some view found)

Now after subclassing the SVGKImageViewadding UITapGestureRecognizer does not work, also this

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

is not called in my subclass. I have also set SVGKImageView's userInteractionEnabled to YES but of no avail.

Can anyone help tell me that why there is no touch/tap event being passed to my SVGKImageView subclass?

Below is my subclass init method

- (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;
}
1

There are 1 best solutions below

5
On

Let order your code:

- (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;
}