Question

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

Was it helpful?

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
    }

}

OTHER TIPS

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);

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.

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:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top