2

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
RayofHope
  • 1,187
  • 2
  • 14
  • 30
  • You can add Gesture Recognizer and can get the x,y of image and let you know where imageis touched – Shashank Kulshrestha Feb 04 '13 at 13:01
  • 1
    @ [Shashank Kulshrestha](http://stackoverflow.com/users/1894318/shashank-kulshrestha) i have addded gesture recognizer but comapring x,y is bit clumsy task i am looking for a better solution if possible in other ways – RayofHope Feb 04 '13 at 13:08
  • 1
    simply -> `image.userInteractionEnabled = NO;` oh, hope the image is not going to get a gesture – Sathya Feb 04 '13 at 13:10
  • @ [codesburner](http://stackoverflow.com/users/445955/codesburner) but i have added tap gesture for image also :( – RayofHope Feb 04 '13 at 13:14

4 Answers4

3

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
    }

}

Justin Mathews
  • 512
  • 4
  • 18
  • **+10** Thanks [Steve Jobs](http://stackoverflow.com/users/1358049/steve-jobs) It works for most touch area but at some point outside of image it still calling button action. – RayofHope Feb 05 '13 at 09:15
  • i just wants the image tap available on the only visible part of image. – RayofHope Feb 05 '13 at 09:20
  • check this link http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage – Justin Mathews Feb 05 '13 at 10:04
0

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

Saurabh Shukla
  • 1,368
  • 3
  • 14
  • 26
  • [Saurabh shukla](http://stackoverflow.com/users/1999876/saurabh-shukla) yaaahhhhh but..that will hide the image part on which i have added tap gesture events to execute also:( – RayofHope Feb 04 '13 at 13:12
  • Whenever user touch the image you can calculate the value of Y coordinate and can compare like this: if(touchValueOfY < (button.frame.origin.y + button.frame.size.width)) { call the button action} else call the image action – Saurabh Shukla Feb 04 '13 at 13:15
  • Bit worked :) but every time buttonTapped will execute in below code else part is not get executed any time if(touchPoint.y < (btndown.frame.origin.y + btndown.frame.size.width)) { NSLog(@"buttonTapped"); } else { NSLog(@"imgTapped"); } – RayofHope Feb 04 '13 at 13:34
0

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.

jackslash
  • 8,550
  • 45
  • 56
0

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:

Bastian
  • 10,403
  • 1
  • 31
  • 40