0

我制作了一个应用程序,您可以在其中摇动手机以打开新视图。所有三个视图,当您在最后一个视图上摇动手机时,您将返回第一个屏幕。当我用自己的.xib 创建新的子类控件视图时,这很好用。但是我想在故事板项目中使用它,我需要改变什么?

非常感谢事先!

这是.H中的代码:

#import <UIKit/UIKit.h>
#import "FirstScreenViewController.h"
#import "SecondScreenViewController.h"

@interface 视图控制器:UIViewController

{

NSInteger currentScreen;
UIViewController* currentController;

}

@结尾

在 .M 中:

#import "ViewController.h"

@implementation 视图控制器

-(void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.

}

#pragma mark shake

-(布尔)可以成为第一响应者

{

return true;

}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

if(motion == UIEventSubtypeMotionShake)

{

如果(当前控制器)

{

[currentController.view removeFromSuperview]; 当前控制器=无;

    }

    switch (currentScreen)

{

        case 0:
            currentController = [[FirstScreenViewController alloc] initWithNibName:@"FirstScreenViewController" bundle:nil];
            break;
        case 1:
            currentController = [[SecondScreenViewController alloc] initWithNibName:@"SecondScreenViewController" bundle:nil];

    }


    if(currentController)

{

        [currentController.view setFrame:self.view.bounds];
        [self.view addSubview:currentController.view];

    }

    currentScreen++;
    if(currentScreen >2)
        currentScreen=0;

}

}

#pragma mark - View lifecycle

-(void)viewDidLoad

{

[超级视图DidLoad]; 当前屏幕 = 0;

}

-(void)viewDidUnload

{

[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;

}

@结尾

4

1 回答 1

1

您需要将所有三个视图控制器添加到情节提要中,并在它们之间设置转场(包括从第三个回到第一个),并在每个场景上附加一个摇晃手势识别器。

每个手势识别器的操作方法告诉视图控制器performSegue:使用适当的 segue 标识符。

于 2012-02-16T07:18:16.600 回答