Umut
Umut

Reputation: 509

How to make invisible buttons in Xcode 5?

I'm trying to make a quiz game. I turn the xib view into Image View to place one of my pre-made question images. After I placed the image I choose a button and place it on the image. However, the button completely supersedes the image. Even after I set the button type to custom, I can't see the image. The idea is just to place four invisible buttons and according to the answer proceed to the next question or display the wrong answer image. Thanks for any help.

Upvotes: 0

Views: 540

Answers (3)

GenieWanted
GenieWanted

Reputation: 4513

You can use touchesBegan method to detect which image was tapped.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if([touch view] == answerImg1) //answerImg1 as your first image
    {
        // Do whatever you want when first image tapped
    }
    else if([touch view] == answerImg2)
    {
         // Do whatever you want when second image tapped
    }
    else if([touch view] == answerImg3)
    {
        // Do whatever you want when third image tapped
    }
    else
    {
        NSLog(@"Other view tapped");
    }


}

Upvotes: 1

Logan
Logan

Reputation: 53112

Why not use a tap gesture recognizer?

Option 1

Step 1: Add tapGestureRecognizer to image views

for (UIImageView * v in @[v1, v2, v3, v4]) {
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
    tap.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:tap];
}

Step 2: Recieve Taps

- (void) handleTap:(UITapGestureRecognizer *)tap {
    if (tap.view == v1) {
        NSLog(@"V1 was tapped");
    }
    else if (tap.view == v2) {
        NSLog(@"V2 was tapped");
    }
    else if (tap.view == v3) {
        NSLog(@"V3 was tapped");
    }
    else if (tap.view == v4) {
        NSLog(@"V4 was tapped");
    }
}

Option 2 - Michael Knudsen's Answer

Here is the alternative method suggested by Michael

Step 1: Add tapGestureRecognizer to image views

UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
tap.numberOfTapsRequired = 1;
[imageViewsParentView addGestureRecognizer:tap];

Step 2: Recieve Taps

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

    if (CGRectContainsPoint(v1.frame, positionInView)) {
        NSLog(@"V1 was tapped");
    }
    else if (CGRectContainsPoint(v2.frame, positionInView)) {
        NSLog(@"V2 was tapped");
    }
    else if (CGRectContainsPoint(v3.frame, positionInView)) {
        NSLog(@"V3 was tapped");
    }
    else if (CGRectContainsPoint(v4.frame, positionInView)) {
        NSLog(@"V4 was tapped");
    }    
}

Upvotes: 2

Michael Knudsen
Michael Knudsen

Reputation: 629

You may be able to solve it by somehow making your view transparent. However, I would suggest another solution: Add a UITapGestureRecognizer and, whenever it senses a tap, check if its coordinates are inside one of the desired areas (corresponding to your imaginary buttons).

Upvotes: 2

Related Questions