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

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

本文介绍了如何通过点击它来动画UIImageview显示全屏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 should shrink back to the original position.

如何实现这一目标?

推荐答案

向视图控制器添加手势识别器。

Add a gesture recognizer to the view controller.

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

Add the gesture Recognizer to your header file

@interface viewController : UIViewController <UIGestureRecognizerDelegate>{
    UITapGestureRecognizer *tap;
    BOOL isFullScreen;
    CGRect prevFrame;
}

在viewDidLoad中添加:

In your viewDidLoad add this:

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

添加以下委托方法:

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

    if (gestureRecognizer == tap) {
        shouldReceiveTouch = (touch.view == yourImageView);
    }
    return shouldReceiveTouch;
}

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

Now you just need to implement your imgToFullScreen method.Make sure you work with the isFullScreen Bool (fullscreen if it is false and back to old size if it's true)

imgToFullScreen方法取决于你想要的方式使图像成为全屏。
一种方法是:
(这是未经测试但应该有效)

The imgToFullScreen method depends on how you want to make the image become fullscreen.One way would be:(this is untested but should work)

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

这篇关于如何通过点击它来动画UIImageview显示全屏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:59