当我们点击图像时,如何将图像与全屏缩放

时间:2015-08-01 13:15:21

标签: ios objective-c

嗨我是初学者在Ios和我的项目中我在我的View控制器上添加了一个图像,当我们点击这个图像然后图像需要缩放全屏尺寸为此我写了一些代码但图像不合适

根据我的代码屏幕显示如下图像但是想要填写该图像整个屏幕帮助我

enter image description here

我的代码如下: -

ViewController1.h

#import <UIKit/UIKit.h>

@interface ViewController1 : UIViewController<UIGestureRecognizerDelegate>
{
    UITapGestureRecognizer *tap;
    BOOL isFullScreen;
    CGRect prevFrame;
}
@property (nonatomic, strong) UIImageView *imageView;

@end

ViewController1.m

#import "ViewController1.h"

@interface ViewController1 ()

@end

@implementation ViewController1

- (void)viewDidLoad
{
    [super viewDidLoad];

    isFullScreen = FALSE;

    tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen)];

    tap.delegate = self;

    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_imageView setClipsToBounds:YES];
    _imageView.userInteractionEnabled = YES;
    _imageView.image = [UIImage imageNamed:@"2.jpg"];

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

    [_imageView addGestureRecognizer:tapper];
    self.view.backgroundColor = [UIColor lightGrayColor];

    [self.view addSubview:_imageView];
}

-(void)imgToFullScreen:(UITapGestureRecognizer*)sender {

    if (!isFullScreen) {

        [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{

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

@end

1 个答案:

答案 0 :(得分:0)

你需要设置所有点按手势。你需要在tapgesture上扩展imageviewsize。或根据您的视图控制器全屏高度和用。改变宽度和高度的大小。

这是tapgesture的例子。

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.cancelsTouchesInView = NO;
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:tapGesture];

-(void)handleTemplateTap:(UIGestureRecognizer *)sender
{
     imageview.frame=CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
}