像Facebook一样查看图像全屏

时间:2014-11-11 00:09:34

标签: ios objective-c

我正在尝试在我的应用中创建一个功能,以便当用户点按图片时,图像会显示为#34;全屏"。点击时的行为应该是将现有图片移动到全屏宽度,以便不会丢失任何图像。我的代码当前移动图像占据手机的高度,因此裁剪宽度。

- (void)viewDidLoad
{
isFullScreen = FALSE;
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen:)];
tap.delegate = self;



_imageView.contentMode = UIViewContentModeScaleAspectFill;
[_imageView setClipsToBounds:YES];
_imageView.userInteractionEnabled = YES;
_imageView.image = [UIImage imageNamed:@"mac4.png"];

UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen:)];
tapper.numberOfTapsRequired = 1;

[_imageView addGestureRecognizer:tapper];

[self.view addSubview:_imageView];
}

-(void)imgToFullScreen:(UITapGestureRecognizer*)sender {
if (!isFullScreen) {
    [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
        //save previous frame
        prevFrame = _imageView.frame;
        [_imageView setFrame:[[UIScreen mainScreen] bounds]];
    }completion:^(BOOL finished){
        isFullScreen = TRUE;
    }];
    return;
}
else{
    [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
        [_imageView setFrame:prevFrame];
    }completion:^(BOOL finished){
        isFullScreen = FALSE;
    }];
    return;
}
}

我做错了什么?我知道以下代码行不允许用户像Facebook一样全屏查看图像:

[_imageView setFrame:[[UIScreen mainScreen] bounds]];

所以基本上,当用户点击图像时,我希望全屏查看图像,同时确保没有图像丢失。 Facebook是我努力寻求的完美典范。

1 个答案:

答案 0 :(得分:0)

您需要使用UIViewContentModeScaleAspectFit代替UIViewContentModeScaleAspectFill。 AspectFill将尝试用图像填充整个imageView,但AspectFit将确保整个图像适合imageView。