点击UIButton时关闭UIView

时间:2015-07-07 13:09:03

标签: ios objective-c uiview

在我的应用中,我有一个水平图像库,当我点击图像时,UIView被称为显示放大的图像。 UIView包含,scrollViewimageviews(动态创建)和UIButton(关闭按钮)以编程方式创建。但是,当我点击关闭按钮UIView时,不会被解雇。 在我的 viewDidLoad 中,我设置了gestureRecognizer

[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)]];

这是 singleTapAction 方法:

- (void)singleTapAction:(UIGestureRecognizer *)singleTap_
{
    MTGalleryPopUp* galleryImage = [[[NSBundle mainBundle] loadNibNamed:@"MTGalleryPopUp" owner:self options:nil] lastObject];
    if(galleryImage.hidden)
    {
        NSLog(@"HIDDEN");
        [galleryImage dismissWithAnimation:YES];
    }
    else
    {
        NSLog(@"NOT HIDDEN");
        [self showGalleryDialog];
    }
}

这是 showGalleryDialog 方法:

- (void)showGalleryDialog
{
    MTGalleryPopUp * galleryView = [[[NSBundle mainBundle] loadNibNamed:@"MTGalleryPopUp" owner:self options:nil] lastObject];

    [galleryView setReferenceVC:self];
    [galleryView initWithTitle:@"Image Gallery"];
    [galleryView setAlpha:0.0f];
    [self.view addSubview:galleryView];
    [UIView beginAnimations:nil context:nil];
    [galleryView setAlpha:1.0];
    [UIView commitAnimations];
}

以下是我如何创建UIView

- (id)initWithTitle:(NSString *)aTitle
{
    CGRect rect = [[UIScreen mainScreen] bounds]; // portrait bounds
    if (self = [super initWithFrame:rect])
    {
        CGRect screenRect = [[UIScreen mainScreen] bounds];
        [self setBackgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.8f]];

        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, TITLE_HEIGHT, screenRect.size.width, 350)];
        [self.scrollView setUserInteractionEnabled:YES];

       CloseButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       [CloseButton addTarget:self
                    action:@selector(dismissWithAnimation:)
          forControlEvents:UIControlEventTouchUpInside];

        [CloseButton setTitle:@"X" forState:UIControlStateNormal];
        [CloseButton.layer setBorderWidth:2];
        [CloseButton.layer setBorderColor:[UIColor whiteColor].CGColor];

        CloseButton.layer.cornerRadius=10;
        CloseButton.layer.masksToBounds = YES;
        [CloseButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        CloseButton.titleLabel.font=[UIFont systemFontOfSize:16];
        CloseButton.frame=CGRectMake(self.frame.size.width-80, 8, 20, 20);
        [self addSubview:CloseButton];

        strURL = [NSString stringWithFormat:@"%@%@?temp_url_sig=%@&temp_url_expires=%lld", URLstorage, CONTAINER_PATH, HMACStr, [@(floor([oneMinLater timeIntervalSince1970])) longLongValue]];

        [mutableURLstorage addObject:strURL];
        [imagesArray addObject:[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]]]];
    }

    CGSize pageSize = CGSizeMake(ITEM_WIDTH, self.scrollView.frame.size.height);
    _pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, screenRect.size.height-30, 320, 36)];
    _pgCtr.backgroundColor=[UIColor grayColor];
    int numberofPage=ceil((float)[imagesArray count]/2.5);
    _pgCtr.numberOfPages= numberofPage;
    self.scrollView.contentSize = CGSizeMake(450*numberofPage, pageSize.height);

    int imgCurrentX = 10;
    for (UIImageView* image in imagesArray)
    {
        @autoreleasepool
        {
            imageview = [[UIImageView alloc] initWithFrame:CGRectMake(imgCurrentX, 45, 200, self.scrollView.frame.size.height - 10)];
                imageview.image = (UIImage*)image;
                [imageview setUserInteractionEnabled:YES];
                [self.scrollView addSubview:imageview];
                imgCurrentX = imgCurrentX + 220;
            }
        }

        [self addSubview:self.scrollView];
    }

    return self;
}

当我在调试时,会发生什么呢?它会回到singleTap_方法并再次重新创建UIView(现在两个UIViews重叠)。我在这里做错了什么?

非常感谢任何帮助。感谢。

2 个答案:

答案 0 :(得分:0)

我看到几个问题:没有保存MTGalleryPopUp;视图不是初始化的;我认为你通过更改' alpha'来解除观点,但请检查隐藏的' singleTapAction中的属性: - 它是不同的东西。

将属性galleryPopUp添加到根控制器:

@property MTGalleryPopUp *galleryPopUp;

它是一个懒惰的初始化对象,添加此方法:

- (MTGalleryPopUp *)galleryPopUp {
  if (!_galleryPopUp) {
    _galleryPopUp = [[[NSBundle mainBundle] loadNibNamed:@"MTGalleryPopUp" owner:self options:nil] lastObject];
  }
  return _galleryPopUp;
}

更改singleTapAction:

- (void)singleTapAction:(UIGestureRecognizer *)singleTap_
{
    if(self.galleryPopUp.alpa == 0)
    {
        NSLog(@"HIDDEN");
        [self showGalleryDialog];
    }
    else
    {
        NSLog(@"NOT HIDDEN");
        [galleryImage dismissWithAnimation:YES];
    }
}

showGalleryDialog中存在问题:

[galleryView initWithTitle:@"Image Gallery"];

它应该在UIView中调用,不是吗?

答案 1 :(得分:0)

您创建了两个“MTGalleryPopUp”类对象。所以更好的方法是在“.h”文件中声明它并在viewDidLoad中初始化它。按照点按即可隐藏和显示。我希望它能解决你的问题