【问题标题】:xcode ios 6 shake motion calls IBaction from previous viewxcode ios 6 摇动从前一个视图调用 IBaction
【发布时间】:2012-11-09 21:13:50
【问题描述】:

我对应用程序开发有点陌生。在 viewController ( VPviewController ) 我有以下代码:

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    if (motion == UIEventSubtypeMotionShake){       
        [self startGame:nil];
    }   
}

在不同的 viewController (VPgameViewController) 中,我有不同的 MotionShake 事件:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    if(event.subtype == UIEventSubtypeMotionShake){
        if(count < 3 ){

            [self changeText:nil];
            AudioServicesPlaySystemSound(1016);
            count++;

         }else{

            count = 0;
            AudioServicesPlaySystemSound(1024);
            UIStoryboard *storyboard = self.storyboard;
            VPpoepViewController *shit = [storyboard instantiateViewControllerWithIdentifier:@"PoepViewController"];
           shit.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
           [self presentViewController:shit animated:YES completion:nil];
        }
    }
}

当我在 VPgameView 中摇动 Iphone 时,它​​也会调用另一个 viewController 摇动事件中的 startGame 函数。

我怎样才能阻止这种情况?

【问题讨论】:

  • 在这两种观点中,我都可以成为第一响应者并辞去第一响应者的职务。但这无济于事。
  • 您想在 Xcode 中还是在 iOS 中检测运动?如果是后者,请不要将 iOS 与 Xcode 混淆。编写 iOS 应用程序不需要 Xcode。

标签: iphone xcode ios4 ios6 motion-detection


【解决方案1】:

听起来您必须取消订阅您的VPViewController 才能在其viewWillDisappear: 函数中接收摇晃事件通知。

通常,如果您希望 viewController 仅在可见时接收某些事件通知,您应该在 viewWillAppear: 函数中订阅通知并在 viewWillDisappear: 函数中取消订阅。

【讨论】:

  • 最好的方法是什么?
  • 要控制 viewController 是否响应摇晃手势,您应该为 viewController 引入 BOOL respondsToShakeGesture 类变量。在motionBeganmotionEnded 函数中,如果此布尔值为假,则立即返回。最后,在viewWillAppear: 函数中将bool 值设置为true,在viewWillDisappear: 函数中将bool 值设置为false。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-16
相关资源
最近更新 更多