简体   繁体   English

如何防止特定视图响应摇动手势?

[英]How do I prevent a specific view from responding to shake gestures?

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. 我的应用程序是一个多视图应用程序,我想在视图2上使用摇动手势。如果在视图2上激活了摇动手势,则必须在MainViewController.m和MyView2Controller.m中编写以下代码才能使用该摇动手势。

Then if i shake the iphone, alert show on both views, and I dont want that. 然后,如果我摇晃iPhone,则在两个视图上都显示警报,而我不希望那样。 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! 我希望仅在视图2中显示警报。因此,如果我在MainViewController.m或视图1上未激活代码,则它在视图2中将不再起作用!

Any help appreciated! 任何帮助表示赞赏!

alt text http://www.freezpic.com/pics/98f1465147344642e3870bb599eba689.jpg 替代文字http://www.freezpic.com/pics/98f1465147344642e3870bb599eba689.jpg

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

    }
}

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: 在这种情况下,您只需要在-viewWillDisappear中为此视图控制器放弃第一响应者状态:

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

The second view controller will then stop responding to shake gestures. 然后,第二个视图控制器将停止响应摇动手势。

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



        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM