mm24
mm24

Reputation: 9586

iOS: detecting touch on image within UIImageView containing multiple images

I got an UIImageView composed of two images with transparancy channel activated.

The view looks something like this: image

I would like to be able to detect precisely the touches within the center circle and distinguish them from the ones in the outern circle.

I am thinking of a collision detection algorithm based the difference between two circles. First test in the outern layer to see if there is a collision at all and then in the inner layer. If in the inner layer then activate inner botton otherwise active outern button.

Any help or suggestion in this?

Shall I create a github repo so everyone could contribute in it?

Upvotes: 0

Views: 515

Answers (1)

Onik IV
Onik IV

Reputation: 5047

Here something than can help you:

    UIImageView *myImageView;
 // In viewDidLoad, the place you are created your UIImageView place this:

myImageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapInView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapInImageView:)];
[myImageView addGestureRecognizer:tapInView];

}

-(void)tapInImageView:(UITapGestureRecognizer *)tap
{
CGPoint tapPoint = [tap locationInView:tap.view];

CGPoint centerView = tap.view.center;

double distanceToCenter = sqrt((tapPoint.x - centerView.x)*(tapPoint.x - centerView.x) + (tapPoint.y - centerView.y)*(tapPoint.y - centerView.y) );
if (distanceToCenter < RADIUS) {
    // It's in center
} else {
    // Touch outside
}

Upvotes: 1

Related Questions