moalemadi
moalemadi

Reputation: 57

showing UiImageView when tapped

I want to add an image view that should be hidden by default, and when the user taps its location, the image should appear.

For example:

If there is a chocolate bar

and I added a small UIImageView on top of the left bottom piece and set it to be hidden, when the user taps that piece of chocolate, the new small image view should come and cover that tapped piece.

This is the code I have

The .h file

#import <UIKit/UIKit.h>


@interface ChompViewController : UIViewController {

    int s;
    int turn;
    IBOutlet UIImageView *sq1;
}

//@property (nonatomic, retain) IBOutlet UIImageView *aq1;

- (IBAction) mainMenu:(id)sender;


@end

The .m file:

#import "ChompViewController.h"
#import "MainMenuViewController.h"

@implementation ChompViewController


// Main Menu button in Start Game view.
- (IBAction) mainMenu:(id)sender {
    MainMenuViewController* mainM = [[MainMenuViewController
    alloc] initWithNibName:@"MainMenuViewController" bundle:nil];

    [self.navigationController pushViewController:mainM animated:YES];

}



// The Game Method.
- (void) startGame {
    s=20;

    while (s>0) {



        // Players' Turn
        if (turn = 1) {


            turn = 0;
        }



        // Computers' Turn
        else {


            turn = 1;
        }

    }
}

@end

So I want to implement the tap and hide in the two 'if' statements under

- (void) StartGame

I hope you can help me, I really need your help..

Thanks

EDIT: I have found a method and made some changes and it works, but it works when tapping any location on the screen and I want it to be triggered when the user taps the exact location of the small UIImageView.

Also, I do not know how to implement this method inside my StartGame method.

This is the code

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

            sq1.hidden = NO;
            [sq1 release];
        }

I know I am asking a lot, I am noob and need some help to improve my knowledge of iPhone development.

===============

EDIT 2..

I have done something else,, but there is something wrong in the code and I do not know what is it..

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




    s=20;
    turn =1;
    while (s>0) {



        // Players' Turn
        if (turn = 1) {

            UITouch *touch = [touches anyObject];
            CGPoint touchLocation = [touch locationInView:self.view];

            if (CGRectContainsPoint(sq1.frame, touchLocation)){
                sq1.hidden = YES;
            }

            turn = 0;
        }




        // Computers' Turn
        else {


            //Computer Here

            turn = 1;
        }
    }   
}

Now when I touch the imageView, the simulator freezes and quits the app.

Upvotes: 0

Views: 615

Answers (2)

Robin
Robin

Reputation: 8357

Right now I can think of two possible ways to do this (probably there are more):

  1. Use the touchesStarted: withEvent: Method and check whether the touch was inside the observed area. If it was, display/unhide the UIImageView

  2. Forget the UIImageView and use transparent UIButtons instead. When the button gets pressed add the image to the instance if UIButton that was pressed.

Upvotes: 1

Bogatyr
Bogatyr

Reputation: 19323

Simplest is to place a custom UIButton with no image and no title text (so it's transparent) on top of the hidden UIImageView location and connect the TouchUpInside action of the button to a method that performs

myImageView.hidden = NO.

Upvotes: 1

Related Questions