Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing touch to next responder or other sub-view iOS

I am sure that this problem will be easily resolved however I am relatively new to iOS development. I am trying to handle passing touch events to children that are lower in the draw order on a UIView. For example -

I create extended UIImageView to create my MoveableImage class. This class is just basically UIImageView that implements the touchesBegan,touchesEnded and touchesMoved-

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


[self showFrame];

//if multitouch dont move
if([[event allTouches]count] > 1)
{
    return;
}



    UITouch *touch = [[event touchesForView:self] anyObject ];

    // Animate the first touch
    CGPoint colorPoint = [touch locationInView:self];

    CGPoint touchPoint = [touch locationInView:self.superview];


    //if color is alpha of 0 , they are touching the frame and bubble to next responder
    UIColor *color = [self colorOfPoint:colorPoint];
    [color getRed:NULL green:NULL blue:NULL alpha:&touchBeganAlpha];
    NSLog(@"alpha : %f",touchBeganAlpha);

    if(touchBeganAlpha > 0)
    {
          [self animateFirstTouchAtPoint:touchPoint];
    }
    else {
        [super.nextResponder touchesBegan:touches withEvent:event];
    }



}

So the end result is basically this- If they are touching the frame of the imageView and not the image inside the other image that will be underneath can possibly respond. See this image for an example.

Passing touch to lower view

So far I have tried next responder however that does not solve the problem. Any help would be greatly appreciated!

Resolved- I stopped checking the alpha on the touchesBegan and touchesMoved. Ovveriding pointInside allowed the UIView to handle that for me.

-(BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *) event
{
  BOOL superResult = [super pointInside:point withEvent:event];
  if(!superResult)
  {
   return superResult;
  }

  if(CGPointEqualToPoint(point, self.previousTouchPoint))
  {
     return self.previousTouchHitTestResponse;
  }else{
     self.previousTouchPoint = point;
  }

  BOOL response = NO;

  //if image is nil then return yes and fall back to super
  if(self.image == nil)
   {
     response = YES;
   }

  response = [self isAlphaVisibleAtPoint:point];
  self.previousTouchHitTestResponse = response;
  return response;





}
like image 939
thebringking Avatar asked Aug 14 '12 15:08

thebringking


1 Answers

You can override instead - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event method for your subclass of UIImageView. (It is a method of uiview that each subclass can override)

The UIView uses this method in hitTest:withEvent: to determine which subview should receive a touch event. If pointInside:withEvent: returns YES, then the subview’s hierarchy is traversed; otherwise, its branch of the view hierarchy is ignored.

Check the source code on github of OBShapedButton. They handle tap event only for the opaque part of a button.

like image 148
tiguero Avatar answered Nov 14 '22 21:11

tiguero