cyclingIsBetter
cyclingIsBetter

Reputation: 17591

IOS: event when an Imageview is touched

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)

Upvotes: 5

Views: 28900

Answers (5)

Jim Driscoll
Jim Driscoll

Reputation: 904

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.

Upvotes: 0

Weles
Weles

Reputation: 1275

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)
}

Upvotes: 2

Tek Yin
Tek Yin

Reputation: 3050

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;
    }];
}

Upvotes: 15

Vladimir
Vladimir

Reputation: 170849

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;
        }];
    

Upvotes: 6

Chris Grant
Chris Grant

Reputation: 2395

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

Upvotes: 4

Related Questions