如何显示黑色背景的全屏图像?

时间:2014-05-27 14:46:11

标签: ios objective-c

我实现了该功能,以便在点按时显示全屏图像。然而,这只会放大图像,但不会显示黑色背景(视图的其他元素仍然在bakcground中)。我怎么能这样做?

//Setup touch up inside of the imageview (in viewdidload)
    userPictImageView.userInteractionEnabled = YES;
    userPictImageView.contentMode = UIViewContentModeScaleAspectFit;
    UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewFullScreenTouchHandler:)];
    tapper.numberOfTapsRequired = 1;
    [userPictImageView addGestureRecognizer:tapper];

//present a full screen image (without black background)
-(IBAction)imageViewFullScreenTouchHandler:(id)sender {
    //goto new ViewController and set the image
    UIImageView *imageView = userPictImageView;

    if (!imageIsFullScreen) {
         [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{

         //save previous frame
         prevFrame = imageView.frame;

        blackView=[[UIView alloc]initWithFrame:self.view.frame];
        [blackView setBackgroundColor:[UIColor blackColor]];
        [self.view addSubview:blackView];

         CGRect frame = self.view.frame;
         [imageView setFrame:frame];
         [blackView addSubview:imageView]; }
             completion:^(BOOL finished){
                    imageIsFullScreen = TRUE;
          }];
        return;
     }
    else{
       self.view.backgroundColor = [UIColor clearColor];

        [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
          [imageView setFrame:prevFrame];
                }completion:^(BOOL finished){
                    imageIsFullScreen = FALSE;
                }];
         return;
       }

}

1 个答案:

答案 0 :(得分:2)

您可以更改为UIViewContentModeScaleToFill,或者更好的是,构建一个黑色背景的UIView,其框架与视图框架匹配。使图像视图成为该黑色视图的子视图。

我做这样的事情。创建一个方法,我可以调用它来缩放和取消缩放imageView。要缩放,请构建背景视图并附加图像。要取消缩放,请执行相反操作,然后销毁背景视图。

由于背景视图将全屏显示,请将手势识别器附加到该屏幕,以便用户可以点按以关闭。

- (void)setUserPictImageViewZoomed:(BOOL)zoom animated:(BOOL)animated {

    UIView *blackView = [self.view viewWithTag:128];
    BOOL isZoomed = blackView != nil;

    // if our current state (isZoomed) matches the desired state (zoomed), then we're done
    if (isZoomed == zoom) return;
    NSTimeInterval duration = (animated)? 0.3 : 0.0;

    if (zoom) {
        blackView = [[UIView alloc] initWithFrame:self.view.frame];
        blackView.backgroundColor = [UIColor blackColor];
        blackView.tag = 128;
        blackView.alpha = 0.0;
        [self.view addSubview:blackView];

        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(unzoom:)];
        [blackView addGestureRecognizer:tapGR];

        UIImageView *zoomImageView = [[UIImageView alloc] initWithImage:self.userPictImageView.image];
        zoomImageView.contentMode = UIViewContentModeScaleAspectFit;
        zoomImageView.tag = 129;
        zoomImageView.frame = [self.userPictImageView convertRect:self.userPictImageView.frame toView:blackView];
        [blackView addSubview:zoomImageView];
        [UIView animateWithDuration:duration animations:^{
            zoomImageView.frame = blackView.bounds;
            blackView.alpha = 1.0;
        }];
    } else {
        UIImageView *zoomImageView = (UIImageView *)[blackView viewWithTag:129];
        [UIView animateWithDuration:duration animations:^{
            zoomImageView.frame = self.userPictImageView.frame;
            blackView.alpha = 0.0;
        } completion:^(BOOL finished) {
            [blackView removeFromSuperview];
        }];
    }
}


- (void)unzoom:(UIGestureRecognizer *)gr {
    [self setUserPictImageViewZoomed:NO animated:YES];
}

我在模拟器中快速测试了它,看起来很漂亮。