以编程方式触发抖动事件iOS [英] Programmatically trigger shake event iOS
问题描述
我已经尝试过以下操作,但是它一直在崩溃...
+(void)shake {
NSLog(@TEST);
UIMotionEventProxy * m = [[NSClassFromString(@UIMotionEvent)alloc] _init];
m-> _subtype = UIEventSubtypeMotionShake;
m-> _shakeState = 1;
[[[UIApplication sharedApplication] keyWindow] motionBegan:UIEventSubtypeMotionShake withEvent:m];
[[[UIApplication sharedApplication] keyWindow] motionEnded:UIEventSubtypeMotionShake withEvent:m];
}
苹果在$ code下的模拟器中做什么硬件> ; Shake Gesture ?
更改UIMotionEventProxy类(添加两个setter方法) 。我简单地添加了两个方法 setShakeState
和 _setSubtype
,如下所示。
- (void)setShakeState:(int)fp8 {
_shakeState = fp8;
}
- (void)_setSubtype:(int)fp8 {
_subtype = fp8;
}
然后我将代码更改为以下...
UIMotionEventProxy * m = [[NSClassFromString(@UIMotionEvent)alloc] _init];
[m setShakeState:1];
[m _setSubtype:UIEventSubtypeMotionShake];
[[UIApplication sharedApplication] sendEvent:m];
[[[UIApplication sharedApplication] keyWindow] motionBegan:UIEventSubtypeMotionShake withEvent:m];
[[[UIApplication sharedApplication] keyWindow] motionEnded:UIEventSubtypeMotionShake withEvent:m];
似乎为模拟器和物理设备完美无缺地工作。 这里是可以下载的主要和头文件,如果有人想看到完整的UIMotionEventProxy文件。
How can I programmatically trigger the shake event in iOS?
I've tried the following but it keeps crashing...
+ (void)shake {
NSLog(@"TEST");
UIMotionEventProxy *m = [[NSClassFromString(@"UIMotionEvent") alloc] _init];
m->_subtype = UIEventSubtypeMotionShake;
m->_shakeState = 1;
[[[UIApplication sharedApplication] keyWindow] motionBegan:UIEventSubtypeMotionShake withEvent:m];
[[[UIApplication sharedApplication] keyWindow] motionEnded:UIEventSubtypeMotionShake withEvent:m];
}
What does apple do in the simulator under Hardware > Shake Gesture
?
Changing the UIMotionEventProxy class (adding two setter methods) seemed to do the trick. I simply added two methods setShakeState
and _setSubtype
, shown below.
-(void)setShakeState:(int)fp8 {
_shakeState = fp8;
}
-(void)_setSubtype:(int)fp8 {
_subtype = fp8;
}
I then changed my code to the following...
UIMotionEventProxy *m = [[NSClassFromString(@"UIMotionEvent") alloc] _init];
[m setShakeState:1];
[m _setSubtype:UIEventSubtypeMotionShake];
[[UIApplication sharedApplication] sendEvent:m];
[[[UIApplication sharedApplication] keyWindow] motionBegan:UIEventSubtypeMotionShake withEvent:m];
[[[UIApplication sharedApplication] keyWindow] motionEnded:UIEventSubtypeMotionShake withEvent:m];
Seems to be working flawlessly for both simulator and physical device. Here are main and header files available for download if anyone wants to see the full UIMotionEventProxy files.
这篇关于以编程方式触发抖动事件iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!