Tutorial :Shake iphone with accelerometer !



Question:

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


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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »