无法在 iPhone 上获取摇动事件

发布于 2024-10-30 13:51:47 字数 1026 浏览 3 评论 0原文

我无法在 iPhone 上获取震动事件。

我在这里关注了其他问题但没有结果。我还尝试遵循 Apple 的 GLPaint 示例,但它看起来与我的源代码完全相同,只有一点点差异。 GLPaint 的源代码 /works/,我的 /doesn't/。

所以,这就是我所拥有的:

Controller.m

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeEnded) name:@"shake" object:nil];
}

ShakingEnabledWindow.m

- (void)shakeEnded {
    NSLog(@"Shaking ended.");
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (motion == UIEventSubtypeMotionShake ) {
        // User was shaking the device. Post a notification named "shake".
        [[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self];
        NSLog(@"Shaken!");
    }
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event { 
}

我的 XIB 有一个窗口,它是一个 ShakingEnabledWindow 和一个对象,即我的控制器。

我在这里没有想法,希望有人能帮我。 :)

I am not being able to get shake events on iPhone.

I have followed other questions here with no result. I also tried following the GLPaint example from Apple, but it seems exactly like my source code, with a small diference. GLPaint's source code /works/, mine /doesn't/.

So, here it is what I have:

Controller.m

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeEnded) name:@"shake" object:nil];
}

ShakingEnabledWindow.m

- (void)shakeEnded {
    NSLog(@"Shaking ended.");
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (motion == UIEventSubtypeMotionShake ) {
        // User was shaking the device. Post a notification named "shake".
        [[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self];
        NSLog(@"Shaken!");
    }
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event { 
}

My XIB has a window, which is a ShakingEnabledWindow and an object, my Controller.

I am running out of ideas here, hope someone can give me a hand. :)

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

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

发布评论

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

评论(3

温暖的光 2024-11-06 13:51:47

NSNotificationCenter 的文档说:

addObserver:选择器:名称:对象:
notificationSelector 选择器
指定消息接收者
发送notificationObserver进行通知
通知发布的内容。这
指定的方法
notificationSelector 必须有一个并且
只有一个参数
(一个实例
NSNotification)。

所以你的 shakeEnded 方法是错误的,因为它不带参数。它应该看起来:

- (void)shakeEnded:(NSNotification*)notiication {
    NSLog(@"Shaking ended.");
}

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeEnded:) name:@"shake" object:nil];
}

Documentation of NSNotificationCenter says:

addObserver:selector:name:object:
notificationSelector Selector that
specifies the message the receiver
sends notificationObserver to notify
it of the notification posting. The
method specified by
notificationSelector must have one and
only one argument
(an instance of
NSNotification).

So your shakeEnded method is wrong as it takes no parameters. It should look:

- (void)shakeEnded:(NSNotification*)notiication {
    NSLog(@"Shaking ended.");
}

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeEnded:) name:@"shake" object:nil];
}
‘画卷フ 2024-11-06 13:51:47

viewDidAppear中,成为第一响应者:

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

并确保你可以成为第一响应者:

- (BOOL)canBecomeFirstResponder {
    return YES;
}

然后就可以实现运动检测了。

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (event.subtype == UIEventTypeMotion){
        //there was motion
    }
}

In viewDidAppear, become the first responder:

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

And make sure you can be first responder:

- (BOOL)canBecomeFirstResponder {
    return YES;
}

Then you can implement the motion detection.

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (event.subtype == UIEventTypeMotion){
        //there was motion
    }
}
<逆流佳人身旁 2024-11-06 13:51:47

我认为您错误地检查了运动类型。您需要检查 event.subtype 而不是 motion

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if ( event.subtype == UIEventSubtypeMotionShake ) {
        // Put in code here to handle shake
    }
}

I think you are incorrectly checking the motion type. You need to check against event.subtype instead of motion:

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if ( event.subtype == UIEventSubtypeMotionShake ) {
        // Put in code here to handle shake
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文