如何通过点击它来动画UIImageview以显示全屏?

mur*_*rze 15 objective-c fullscreen uiimageview ios5

我在UITableviewCell中有一个UIImageView.当点击它时,UIImageView应该动画显示全屏.当图像全屏时拍摄,它应缩回原始位置.

怎么能实现这一目标?

arn*_*app 28

将手势识别器添加到视图控制器.

将手势识别器添加到头文件中

@interface viewController : UIViewController <UIGestureRecognizerDelegate>{
    UITapGestureRecognizer *tap;
    BOOL isFullScreen;
    CGRect prevFrame;
}
Run Code Online (Sandbox Code Playgroud)

在viewDidLoad中添加以下内容:

isFullScreen = false;
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen)];
tap.delegate = self;
Run Code Online (Sandbox Code Playgroud)

添加以下委托方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
{
    BOOL shouldReceiveTouch = YES;

    if (gestureRecognizer == tap) {
        shouldReceiveTouch = (touch.view == yourImageView);
    }
    return shouldReceiveTouch;
}
Run Code Online (Sandbox Code Playgroud)

现在你只需要实现你的imgToFullScreen方法.确保你使用isFullScreen Bool(全屏如果是假的,如果它是真的那么回到旧的大小)

imgToFullScreen方法取决于您希望如何使图像成为全屏.一种方法是:(这是未经测试但应该工作)

-(void)imgToFullScreen{
    if (!isFullScreen) {
        [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
            //save previous frame
            prevFrame = yourImageView.frame;
            [yourImageView setFrame:[[UIScreen mainScreen] bounds]];
        }completion:^(BOOL finished){
            isFullScreen = true;
        }];
        return;
    } else {
        [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
            [yourImageView setFrame:prevFrame];
        }completion:^(BOOL finished){
            isFullScreen = false;
        }];
        return;
    }
}
Run Code Online (Sandbox Code Playgroud)


jer*_*rik 6

来自@ AzzUrr1的代码,小错误更正(括号)和tapper实现略有不同.

为我工作.现在用scrollView实现它会很棒,如果图片更大,用户可以放大/缩小..有什么建议吗?

ViewController.h

#import <UIKit/UIKit.h>

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

@end
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)