摇动检测iPhone 3.0不工作 [英] Shake Detection iPhone 3.0 not working
本文介绍了摇动检测iPhone 3.0不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个ViewController,可以触发一个动作的按钮。我想用一个摇动事件替换按钮,所以我已经google了它,并创建了一个从UIView
继承的ShakeDetector类,我的实现如下: / p>
@implementation ShakeDetector
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)事件
{
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(motion == UIEventSubtypeMotionShake)
{
//用户正在摇动设备。发布名为摇的通知。
// [[NSNotificationCenter defaultCenter] postNotificationName:@spinobject:self];
NSLog(@sss);
}
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
}
@end
但是我无法使其工作...任何帮助? p>
谢谢
解决方案
Put:
- (BOOL)canBecomeFirstResponder
{
return YES;
}
和您的观点:
$ b $ ($)$ {code> - (void)viewDidAppear:(BOOL)动画{
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewDidDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewDidDisappear:animated];
}
您还可以将其写入viewWillAppear和viewWillDisappear
I have a ViewController that works perfectly with a button that trigger an action. I would like to replace the button with a shake event so I've googled it around and created a ShakeDetector class that ineherits from UIView
and my implementation is as follow:
@implementation ShakeDetector
- (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:@"spin" object:self];
NSLog(@"sss");
}
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
}
@end
But I can't make it work... any help?
Thanks
解决方案
Put :
-(BOOL)canBecomeFirstResponder
{
return YES;
}
and for your view :
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewDidDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewDidDisappear:animated];
}
You can also write it in viewWillAppear and viewWillDisappear
这篇关于摇动检测iPhone 3.0不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文