5

I have an ImageView with a png and I want to do this: when someone touch this imageview it's alpha change to 0.0, is it possible? (all without buttons)

cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
  • 1
    Why eliminate buttons? You can modify their properties to make them look exactly like an image while retaining all of the functionality that you get with a button. – George Johnston Oct 12 '11 at 12:56
  • possible duplicate of [how can i detect the touch event of an UIImageView](http://stackoverflow.com/questions/855095/how-can-i-detect-the-touch-event-of-an-uiimageview) – vikingosegundo Oct 12 '11 at 13:02

5 Answers5

15

you can use UITapGestureRecognizer added to the UIImageView via addGestureRecognizer

snippets:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTaped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[iv addGestureRecognizer:singleTap];
[iv setUserInteractionEnabled:YES];

and

- (void)imageTaped:(UIGestureRecognizer *)gestureRecognizer {
    [UIView animateWithDuration:0.5 animations:^(void){
         imageView.alpha = 0.0f;
    }];
}
Tek Yin
  • 3,011
  • 3
  • 25
  • 42
6

Yes, it is possible. For example you can do that with following steps:

  1. set image view's userInteractionEnabled property to YES - so it will receive touch events
  2. add UITapGestureRecongnizer to it
  3. in gesture handler set view's alpha to 0.0, you can do that with animation as well:

    [UIView animateWithDuration:0.5 animations:^(void){
            imageView.alpha = 0.0f;
        }];
    
Vladimir
  • 170,431
  • 36
  • 387
  • 313
4

There are already lots of questions like this. Searching with google gave me the following:

touches event handler for UIImageView

UIImageView Touch Event

how can i detect the touch event of an UIImageView

Community
  • 1
  • 1
Chris Grant
  • 2,463
  • 23
  • 27
2

The code in Swift

In my case I implemented the tap gesture for an image click

1 - Link the image with the ViewController, by drag and drop

@IBOutlet weak var imgCapa: UIImageView!

2 - Instance the UITapGestureRecognizer in ViewDidLoad method:

override func viewDidLoad() {
    super.viewDidLoad()

    //instance the UITapGestureRecognizer and inform the method for the action "imageTapped"
    var tap = UITapGestureRecognizer(target: self, action: "imageTapped")

    //define quantity of taps
    tap.numberOfTapsRequired = 1
    tap.numberOfTouchesRequired = 1

    //set the image to the gesture 
    imgCapa.addGestureRecognizer(tap)
}

3 - Create the method to do what do you want when the image clicked

func imageTapped(){
    //write your specific code for what do you want     

    //in my case I want to show other page by specific segue 
    let sumario = self.storyboard?.instantiateViewControllerWithIdentifier("sumarioViewController") as SumarioViewController

    self.performSegueWithIdentifier("segueSumarioViewController", sender: sumario)
}
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Weles
  • 1,275
  • 13
  • 17
0

In recent Xcode, this is pretty easy. Go into the storyboard, in the object library search for "gesture", drag the one you want onto the image view. You can then treat the gesture object in the view hierarchy as the thing being tapped, i.e. control-drag from there to your view controller to connect the event handler.

Once there you can set the alpha as you like, although if you're trying to essentially remove the image view, you should set imageView.hidden = true / imageView.hidden = YES instead, because that will stop it receiving events.

Jim Driscoll
  • 894
  • 11
  • 8