简体   繁体   中英

IOS How to shake to change View

I've already know how to catch the shake gesture with the code below

    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if ( event.subtype == UIEventSubtypeMotionShake )
    {
        // Put in code here to handle shake
        NSLog(@"Device Shaked");       


    }

    if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] )
        [super motionEnded:motion withEvent:event];
}

The NSLog shows that it did received the shake

but how to push another view,or back to the last view, as we know the code below can only use in ViewController class instead of View class, this Leavesview is handle by a LeavesviewController, and I use this viewController in another PDFViewController to display, so how could I make it jump to another view or dismiss back???

 UIViewController *myView = [[UIViewController alloc]init];
   [self.navigationController pushViewController:myView animated:YES];
See this complet Code......


      - (BOOL) canBecomeFirstResponder {
            return YES;
        }

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

            if (event.subtype == UIEventSubtypeMotionShake)
            {
                // Work which you want
            }
        }



    also dont forget these two methods...

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

       }


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

Ok, Obviously I am just a starter 2 months ago. Also, obviously the answer is the Delegation ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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