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

How to animate a UIImageview to display fullscreen by tapping on it?(如何通过点击动画 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(如果为 false,则为全屏,如果为 true,则返回旧尺寸)

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 以显示全屏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

AWS DynamoDB Batch Get Request - iOS(AWS DynamoDB 批量获取请求 - iOS)
DynamoDB auto incremented ID amp; server time (iOS SDK)(DynamoDB 自动递增 ID amp;服务器时间(iOS SDK))
dynamodb scanexpression with scan filter in objective-c(在objective-c中带有扫描过滤器的dynamodb scanexpression)
Getting full access to DynamoDB from my ios app using AWS Cognito Developer Identities(使用 AWS Cognito 开发人员身份从我的 ios 应用程序获得对 DynamoDB 的完全访问权限)
iOS framework with dependencies(具有依赖项的 iOS 框架)
Can#39;t change target membership visibility in Xcode 4.5(无法更改 Xcode 4.5 中的目标成员身份可见性)