首页 > 解决方案 > 如何关闭全屏 UIImageView?

问题描述

在我的应用程序中单击按钮时,我正在创建这样的 UIImageView

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

但是,当它被点击时,我如何消除相同的视图?

标签: objective-cuiimageview

解决方案


您可以添加UITapGestureRecognizer您的UIImageView并在其操作中删除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;

现在只需删除UIImageView.UITapGestureRecognizer

- (void)dismissTapGesture:(UITapGestureRecognizer*)gesture {
    [gesture.view removeFromSuperview];
}

推荐阅读