How to animate a UIImageview to display fullscreen by tapping on it?

后端 未结 5 1853
眼角桃花
眼角桃花 2020-12-07 22:16

I have an UIImageView in a UITableviewCell. When it is tapped, the UIImageView should animated to be displayed fullscreen. When the image is tapped when it is fullscreen it

5条回答
  •  情书的邮戳
    2020-12-07 22:33

    The code from @AzzUrr1, small error corrections (brackets) and tapper implemented slightly different.

    Worked for me. Now it would be great to have this implemented with a scrollView, that the user can zoom in/out if the picture is bigger.. Any suggestion?

    ViewController.h

    #import 
    
    @interface ViewController : UIViewController  {
        UITapGestureRecognizer *tap;
        BOOL isFullScreen;
        CGRect prevFrame;
    }
    @property (nonatomic, strong) UIImageView *imageView; 
    
    @end
    

    ViewController.m

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        isFullScreen = FALSE;
        tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen)];
        tap.delegate = self;
        self.view.backgroundColor = [UIColor purpleColor];
    
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 200)];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        [_imageView setClipsToBounds:YES];
        _imageView.userInteractionEnabled = YES;
        _imageView.image = [UIImage imageNamed:@"Muppetshow-2.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;
        }
    }
    

提交回复
热议问题