Shake iphone with accelerometer !

泄露秘密 提交于 2019-12-12 19:04:32

问题


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];

        }
    }
}

回答1:


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];
}

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



来源:https://stackoverflow.com/questions/3414800/shake-iphone-with-accelerometer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!