user2019219
user2019219

Reputation: 61

Objective C - Tap Gesture counting

I have some imageViews on my View, now I want to change the background.color of the image in my imageView. By clicking second time on the same imageView it should change to the first image.

How I can do this ?

Upvotes: 0

Views: 355

Answers (2)

Samkit Jain
Samkit Jain

Reputation: 2523

You can have a Variable to check how many times your ImageView has been touched. This can be changed in :

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

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;


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

{

    UITouch *touch = [touches anyObject];
    if (touch.view == imageView) {
        // Make that variable ++;
        if (variable == 2){
            imageView.image = [UIImage imageNamed:@"a.png"];
        }
    }
}

Upvotes: 0

Mundi
Mundi

Reputation: 80265

Keep a BOOL or enum ivar to keep track of the image state. When reacting to the tap, exchange the image.

You could have a custom subclass of image view to keep this state always associated with the right object.

// .h
typedef enum { StateOriginal, StateFlipped } FlipState;

@interface FlippableImageView : UIImageView
@property (nonatomic, assign) FlipState state;
@end

// in view controller
if (flipImageView.state == StateOriginal) {
  flipImageView.image = imageFlipped;
  flipImageView.state = StateFlipped;
}
else { 
  flipImageView.image = imageOriginal;
  flipImageView.state = StateOriginal;
}

The class could also hold both images and run a nice animation...

Upvotes: 1

Related Questions