双击切换全屏动作 [英] Toggle Fullscreen action with Double Tap

查看:94
本文介绍了双击切换全屏动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在寻找一种可通过双击来实现全屏"操作的方法,但我没有成功!

Looking for a way to implement a "full-screen" action reversible with double tap but I did not succeed!

更详细地讲,有2个UIView: -topViewContainer -bottomViewContainer

More in detail, there are 2 UIView : - topViewContainer - bottomViewContainer

当我双击超级视图时,视图"bottomViewContainer"扩展到全屏,然后再次双击,视图将返回其原始大小.

When I double-tap on the superview, the view "bottomViewContainer" extends to full-screen, and I re-double tap, the view returns to its original size.

它应该可以在纵向和横向模式下工作!

It should work in portrait mode and Landscape Mode!

这是我到目前为止所做的:

This is what I've done until now:

-(void)handleDoubleTap:(UITapGestureRecognizer *)sender {
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
    if (sender.numberOfTapsRequired == 2){

         NSLog(@"if gesture up - LS");

        [UIView animateWithDuration:0.5
                              delay:0.1
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             topContainerView.frame = CGRectMake(0.0, -160.0, 480.0, 244.0);
                             bottomContainerView.frame = CGRectMake(0.0, 0.0, 480.0, 300.0);}
                         completion:^(BOOL finished){
                           NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height);                                   
                         }];

    } else if (sender.numberOfTapsRequired == 2) {

        NSLog(@"else if gesture down - LS");

        [UIView animateWithDuration:0.5
                              delay:0.1
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             topContainerView.frame = CGRectMake(0.0, -160.0, 480.0, 244.0);
                             bottomContainerView.frame = CGRectMake(0.0, 84.0, 480.0, 216.0);}
                         completion:^(BOOL finished){
                            NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height); 
                         }];
    }
}
else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) {
    if (sender.numberOfTapsRequired == 2) {

        NSLog(@"if gesture down - PT");

        [UIView animateWithDuration:0.5
                          delay:0.1
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         topContainerView.frame = CGRectMake(0.0, 0.0, 320.0, 244.0);
                         bottomContainerView.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 640.0);
                     }
                     completion:^(BOOL finished){
                         NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height);
                     }];
     }
    else if (sender.numberOfTapsRequired == 2) {

        NSLog(@"else if gesture up - PT");

        [UIView animateWithDuration:0.5
                              delay:0.1
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             topContainerView.frame = CGRectMake(0.0, 0.0, 320.0, 244.0);
                             bottomContainerView.frame = CGRectMake(0.0, 244.0, self.view.frame.size.width, 216.0);
                         }
                         completion:^(BOOL finished){
                            NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height); 
                         }];

    }
}        

}

推荐答案

尝试以下代码:

#import "ViewController.h"

@interface ViewController (){
    UIView *topContainerView;
    UIView *bottomContainerView;

    CGRect initialTopContainerView;
    CGRect initialBottomContainerView;

    BOOL isFullScreen;
}

@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

        CGSize result = [[UIScreen mainScreen] bounds].size;

        topContainerView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, result.width, result.height/2)];
        topContainerView.backgroundColor = [UIColor redColor];
        topContainerView.tag = 1;
        [self.view addSubview:topContainerView];

        bottomContainerView = [[UIView alloc]initWithFrame:CGRectMake(0.0, result.height/2, result.width, result.height/2)];
        bottomContainerView.backgroundColor = [UIColor blueColor];
        bottomContainerView.tag = 2;
        [self.view addSubview:bottomContainerView];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];
        tap.numberOfTapsRequired = 2;
        [bottomContainerView addGestureRecognizer:tap];

        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];
        tap2.numberOfTapsRequired = 2;
        [topContainerView addGestureRecognizer:tap2];

        initialTopContainerView = bottomContainerView.frame;
        initialBottomContainerView = topContainerView.frame;

        isFullScreen = false;

    }
    return self;
}

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

    CGSize result = [[UIScreen mainScreen] bounds].size;

    int heightSreen = result.height;
    int widthSreen = result.width;

    if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
        heightSreen = result.width;
        widthSreen = result.height;
    }

    if (sender.numberOfTapsRequired == 2) {

        CGRect newFrameTop;
        CGRect newFrameBottom;

        isFullScreen = !isFullScreen;

        if (isFullScreen) {

            if (sender.view.tag == 1) {
                newFrameTop = CGRectMake(0, 0, widthSreen, heightSreen);
                newFrameBottom = CGRectMake(0, heightSreen, widthSreen, 0);
            }else{
                newFrameTop = CGRectMake(0, 0, widthSreen, 0);
                newFrameBottom = CGRectMake(0, 0, widthSreen, heightSreen);
            }

            [UIView animateWithDuration:0.5
                                  delay:0.1
                                options:UIViewAnimationOptionBeginFromCurrentState
                             animations:^{
                                 topContainerView.frame = newFrameTop;
                                 bottomContainerView.frame = newFrameBottom;
                             }completion:nil];

        }else{

            [UIView animateWithDuration:0.5
                                  delay:0.1
                                options:UIViewAnimationOptionBeginFromCurrentState
                             animations:^{
                                 topContainerView.frame = CGRectMake(0.0, 0.0, widthSreen, heightSreen/2);
                                 bottomContainerView.frame = CGRectMake(0.0, heightSreen/2, widthSreen, heightSreen/2);
                             }completion:nil];

        }

    }

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

这篇关于双击切换全屏动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆