I am creating a UIImageView like this when a button is clicked in my app
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:selectedVariantDetail[@"fcVariantImageUrl"]]];
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(0,0,screenWidth,screenHeight)];
dot.image=[UIImage imageWithData:imageData];
dot.tag = 2;
[self.view addSubview:dot];
But how do I dismiss the same view when it is tapped on?
You can add UITapGestureRecognizer
in your UIImageView
and on its action remove the UIImageView
.
//Add tap Gesture
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissTapGesture:)];
[dot addGestureRecognizer:tapGesture];
//Set userInteractionEnabled property of imageView to true because by defaults its false
dot.userInteractionEnabled = YES;
Now simply remove UIImageView
in action method of UITapGestureRecognizer
.
- (void)dismissTapGesture:(UITapGestureRecognizer*)gesture {
[gesture.view removeFromSuperview];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.