RayofHope
RayofHope

Reputation: 1187

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

Upvotes: 2

Views: 1117

Answers (4)

Justin Mathews
Justin Mathews

Reputation: 512

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
    }

}

Upvotes: 3

Bastian
Bastian

Reputation: 10433

You can solve the problem by writing a hittest function for the overlapping button.

In the hittest you have to decide which button was hit, by checking the coordinates.

http://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instm/UIView/hitTest:withEvent:

Upvotes: 0

jackslash
jackslash

Reputation: 8570

Just avoid overlapping the button with the image in that way. It looks terrible.

For a more long term solution I recommend making friends with UX designer and a Graphic Artist. If you're lucky that may even be the same person.

Upvotes: 0

Saurabh Shukla
Saurabh Shukla

Reputation: 1398

You can add a new transparent button which would be overlapping your image part and existing button both, having the frame

CGRectMake(button.frame.origin.x, button.frame.origin.y, button.frame.size.width, button.frame.size.height);

Upvotes: 0

Related Questions