开发者

Shake iphone with accelerometer !

开发者 https://www.devze.com 2023-01-10 03:39 出处:网络
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 , iPaddo not support this code !!! but works on all device开发者_运维百科s

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 device开发者_运维百科s .. 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];

        }
    }
}


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消