0

I'm pretty new to IOS development. Let me explain my problem first.

In Adobe Flash, there is an mouse event that enables me to detect mouse enter to a movieclip. So for example suppose i have a main container movie clip and inside it i have bunch of objects. If i drag my mouse over those objects, they will -for example- change their color.

In IOS, I have a main view used as a container and inside it i'm populating couple of UIViews with their colors set to some color -blue-. What i'm trying to achieve is, when i tap and start moving my finger around, i want the ones under my finger to change their color. I'm using the following code:

for (int i=0;i<8;i++)
{
    for(int j=0; j<8; j++)
    {
      UIPanGestureRecognizer * gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureCaptured:)];
      UIView * test = [[UIView alloc] initWithFrame:CGRectMake(i*34, j*34, 32, 32)];
      [test setBackgroundColor:[UIColor blueColor]];
      [self.view addSubview:test];
      [test addGestureRecognizer:gesture];
    }
}

And for the handler:

-(void) panGestureCaptured:(UIPanGestureRecognizer*)sender
{
    [sender.view setBackgroundColor:[UIColor redColor]];
}

But the problem is that, only the first one's color is changed. And of course there may be performance problem with this: Although only one capture and color change is sufficient; the handler/action is called multiple times when my finger is inside a square.

So, any suggestion how to approach this type of problem?

Thanks in advance.

Lexicon
  • 473
  • 1
  • 3
  • 15

1 Answers1

1

The problem is that only the gesture recognizer in the first subview is reacting, so the touches in the gesture that are not in its area are having no effect. You could try this:

-(void) panGestureCaptured:(UIPanGestureRecognizer*)sender
{
    for(NSUInteger i=0;i<[sender numberOfTouches];i++)
    {
        CGPoint touchPt = [sender locationOfTouch:i inView:self.view];
        for(UIView *subV in [self.view subviews])
        {   
            if(CGRectContainsPoint(c.frame, touchPt))
            {   
                subV.backgroundColor = [UIColor whiteColor]; //or whatever
                break; //assumes no overlapping
            }
        }
    }
}
Rich Tolley
  • 3,812
  • 1
  • 30
  • 39