gpt4 book ai didi

iOS:如何识别连续摇动事件

转载 作者:行者123 更新时间:2023-11-29 02:57:55 25 4
gpt4 key购买 nike

我试过使用函数

- (BOOL) canBecomeFirstResponder{
return YES;
}
- (void) viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeDetected:) name:@"NOTIFICATION_SHAKE" object:nil];

[self.view becomeFirstResponder];
[super viewWillAppear:animated];
}

- (void) viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.view resignFirstResponder];
[super viewDidDisappear:animated];
}


- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.subtype == UIEventSubtypeMotionShake) {
//action if detect shake
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION_SHAKE" object:self];

}

但是当进行连续摇动时,该功能不会自动检测摇动事件。这里的问题是如何定义连续震动事件?

最佳答案

通常当您摇动设备时,系统会向您的 View Controller 发送事件:motionBegan -> motionEnded。

当您连续摇动时,系统会多次发送消息 motionBegan -> motionCancelled(因为不再是单次摇动)。

您可以尝试玩这个事件序列,但我认为您必须使用加速度计,请参阅 this answer (摇骰子)。

关于iOS:如何识别连续摇动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23671809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com