用加速度计摇动iPhone! [英] Shake iphone with accelerometer !

查看:43
本文介绍了用加速度计摇动iPhone!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,出于某些原因,我无法使用摇晃API,所以我通过加速度计使用摇晃设备,但是我不知道为什么iPhone 4和iPad不支持此代码!但可以在所有设备上使用..我不知道发生了什么!这是我的代码:

Hi every one for some reasons i can't use shake API , so iam using shake device via accelerometer , but i don't why iPhone 4 , iPad do not support this code !!! but works on all devices .. i don't know what's going on ! here is my code :

#define kAccelerationThreshold        2.2
#define kUpdateInterval               (1.0f/10.0f)

@interface info : UIViewController  <UIAccelerometerDelegate> {

}
@end

~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~

@implementation info


- (void)viewDidLoad {
    UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
    accel.delegate = self;
    accel.updateInterval = kUpdateInterval;


    [super viewDidLoad];
}


#pragma mark -
- (void)accelerometer:(UIAccelerometer *)accelerometer 
        didAccelerate:(UIAcceleration *)acceleration {
   {
        if (acceleration.x > kAccelerationThreshold 
            || acceleration.y > kAccelerationThreshold
            || acceleration.z > kAccelerationThreshold) {

//What do you want to do !

                self.view.backgroundColor = [UIColor orangeColor];

        }
    }
}

推荐答案

如果您无法在iPhone上使用摇动API,则可能尚未将视图控制器设置为viewDidAppear上的第一响应者.在您的视图控制器上实现此方法:

If you can't use the shake API on your iPhone, you probably haven't set your view controller as the first responder on viewDidAppear. Implement this method on your view controller:

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

现在,您将可以使用摇摇API.希望能有所帮助:)

Now you will be able to use the shake API. Hope that helps :)

这篇关于用加速度计摇动iPhone!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆