【问题标题】:How can I use shake gesture when using storyboards?使用情节提要时如何使用摇动手势?
【发布时间】:2012-02-15 17:19:08
【问题描述】:

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

非常感谢您!

这是 .H 中的代码:

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

@interface ViewController : 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

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

{

return true;

}

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

{

if(motion == UIEventSubtypeMotionShake)

{

如果(当前控制器)

{

[currentController.view removeFromSuperview]; currentController=nil;

    }

    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

{

[超级viewDidLoad]; 当前屏幕 = 0;

}

-(void)viewDidUnload

{

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

}

@结束

【问题讨论】:

  • 如果您的代码格式正确,您更有可能获得帮助。
  • 通过粘贴、选择并点击问题编辑器上方的 {} 按钮来格式化代码。或者,缩进 4 个空格。

标签: xcode view storyboard gesture shake


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    相关资源
    最近更新 更多