Search code examples
iphoneiostouchuser-interactionimage-masking

How to get touch behind the imageview on transparent part of image on iOS?


Here I want to get the touch for button action event on button tap - B

and when image is tapped (on visible area of image) the tap gesture method will call

but problem is image is on the button and its covers some part of the button I wants to execute the button action event on that covered portion - A

I have tried but not able to get the button action on covered portion :(

Image Masking


Solution

  • Put a transparent button over that portion

    update

     -(void)singleTapImageview:(UITapGestureRecognizer *)gesture
     {
        NSLog(@"single-tap");
        CGPoint touchLocation = [gesture locationInView:self.imageViewParent];
        float x = touchLocation.x;
        float y = touchLocation.y;
    
        //Using gesture recogniser you can get the current position of touch
    
        UIImage* image = imageViewParent.image;
        CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
        UInt8 *pixels = (UInt8 *)CFDataGetBytePtr(data);
    
        UIGraphicsBeginImageContext(image.size);
        [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1);
        CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor redColor].CGColor);
        int index = ((x + (y * (int)image.size.width)) * 4) + 3;
        CGFloat alpha = pixels[index];
        if (alpha == 0)
        {
            // call button acton
        }
    

    }