MotionEnded 未被调用(无视图控制器)

发布于 2024-09-15 12:25:16 字数 648 浏览 7 评论 0原文

我想我已经做了所有应该检测到的震动,但是 motionEnded:withEvent: 从未被调用。 (一个问题是我没有 UIViewController - 我的应用程序基于“OpenGL ES App”模板。)

我添加了 application.applicationSupportsShakeToEdit = YES; 到我的 application:didFinishLaunchingWithOptions:,以及

- (BOOL)canBecomeFirstResponder { return YES; }

EAGLView.m(确实被调用),以及 [self comeFirstResponder];initWithCoder: (并且也尝试过其他各种地方)。

但调试器从未命中

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

我是否错过了某些步骤?我必须有一个控制器吗?

(我在 iPad 模拟器中使用 iOS 3.2。)

I think I've done all I should to detect a shake, but motionEnded:withEvent: never gets called. (One wrinkle is that I don't have a UIViewController - my app is based on the "OpenGL ES App" template.)

I've added application.applicationSupportsShakeToEdit = YES; to my application:didFinishLaunchingWithOptions:, and

- (BOOL)canBecomeFirstResponder { return YES; }

to EAGLView.m (which does get called), and [self becomeFirstResponder]; to initWithCoder: (and have tried various other places too).

But the debugger never hits

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

Am I missing some step? Do I have to have a controller?

(I'm using iOS 3.2 in the iPad simulator.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帅哥哥的热头脑 2024-09-22 12:25:16

您必须将其添加到您的控制器中:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

-(BOOL)becomeFirstResponder
{
    return YES;
}

You must add this to your controller:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

-(BOOL)becomeFirstResponder
{
    return YES;
}
离鸿 2024-09-22 12:25:16

UIResponder 链处理摇动通知的方式令人讨厌。似乎 UIWindow 总是收到通知,然后子响应者可能会或可能不会取决于链中它们上面的内容。我创建了一个 UIWindow 子类,并将其定义为我的窗口类,如下所示:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{
    if (event.type == UIEventSubtypeMotionShake) 
        [[NSNotificationCenter defaultCenter] postNotificationName:@"UIEventSubtypeMotionShakeEnded" object:nil];
}

然后,对于任何需要摇动通知的视图,我只需让它们将自己添加为 UIEventSubtypeMotionShakeEnded 的观察者 事件,他们每次都得到它。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeNotification:)
                                             name:@"UIEventSubtypeMotionShakeEnded" object:nil];

The way the UIResponder chain works with the shake notification is obnoxious. Seems that UIWindow always gets the notification, and then sub-responders may or may not depending on whats above them in the chain. I created a UIWindow subclass, and defined it as my window class with the following:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{
    if (event.type == UIEventSubtypeMotionShake) 
        [[NSNotificationCenter defaultCenter] postNotificationName:@"UIEventSubtypeMotionShakeEnded" object:nil];
}

Then, for any views that wanted the shake notifications, I simply had them add themselves as an observer to the UIEventSubtypeMotionShakeEnded event, and they got it every time.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeNotification:)
                                             name:@"UIEventSubtypeMotionShakeEnded" object:nil];
染墨丶若流云 2024-09-22 12:25:16

在您的视图进入视图层次结构之前,您无法成为第一响应者。

You can't become the first responder before your view is in the view hierarchy.

东北女汉子 2024-09-22 12:25:16

更新到最新版本的 SDK 神奇地解决了这个问题。 [耸肩]

Updating to the latest version of the SDK magically fixed the problem. [Shrug]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文