views:

141

answers:

1

I have a grid of images/buttons, and I want the user to be able to drag their finger over the images/buttons such that when their finger touches each image/button an event is called.

How do I do this???

The best example I can think of now is the Contacts app, how you drag your finger down the list of letters (on the right) and as you touch each letter it jumps to that part of the contacts list.

A: 

You're going to want to implement -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event in your UIViewController. This method is called any time a touch moves on the screen. You can then either use the coordinates (use [myUITouch locationInView:self.view]) or [self.view.layer hitTest:[myUITouch locationInView:self.view]] to get the image/button the touch occurred in and work from that.

For example, if I have a row of ten images, each 32 pixels by 32 pixels, and I want to record when each of them is touched, I could do something like the following:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGSize buttonSize = myButton.bounds.size;
    CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
    if (touchPoint.y < buttonSize.y) {
        NSInteger buttonNumber = touchPoint.x/buttonSize.x;
        NSLog(@"Button number %d pushed", buttonNumber);
    }
}

Note this assumes multitouch is disabled or it will randomly pick one touch to record

David Kanarek