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)
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;
}];
}
Yes, it is possible. For example you can do that with following steps:
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;
}];
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
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With