views:

354

answers:

2

Hi. I use shake gesture for my application but i have problem!

My application is a multiview application and i want use shake gesture on the view 2. If i active shake gesture on view 2, I have to write the code below in the MainViewController.m and MyView2Controller.m to use the shake gesture.

Then if i shake the iphone, alert show on both views, and I dont want that. I want the alert to only show up in view 2. So if i inactive the code on the MainViewController.m or view 1, it no longer works in view 2!

Any help appreciated!

alt text

Here is my code :

-(BOOL)canBecomeFirstResponder {
    return YES;
}

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

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

{
    if (event.subtype == UIEventSubtypeMotionShake)
    {
     UIAlertView *alet =[[UIAlertView alloc]initWithTitle:nil message:@"Shake shake shake" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
     [alet show];
     [alet release];
     self.view.backgroundColor = [UIColor orangeColor];

    }
}
+2  A: 

I assume that your code is from the view controller for your second view (the one you want to respond to the shake gesture). In that case, you simply need to resign the first responder status for this view controller in -viewWillDisappear:

- (void)viewWillDisappear:(BOOL)animated 
{
    [super viewWillDisappear:animated];
    [self resignFirstResponder];
}

The second view controller will then stop responding to shake gestures.

Brad Larson
hi brad ! i changed viewDidAppear to viewWillDisappear and doesn't any happen . here is my sample code can you solve my problem ? :)PLEASE http://rapidshare.com/files/303744589/ShakeIt.zip.html
Momeks
i found something ! my code only works in MainviewController (my orginal appViewController) and if i don't write any code on the MyView2Controller.m or any viewController files .... Alert only show with MainviewController on the any view !
Momeks
A: 

OK i solved my problem with this 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];



        }
    }
}
Momeks