Slide out navigation through swipes for UIView to

2019-05-28 23:55发布

问题:

On my research regarding this navigation technique, I've found this post by Nick Harris which is a good start I suppose. However, what I want is slightly different from this. You can think of it as iOS' Notification Center when you just have to swipe from the top to show the view and just swipe back to hide it again. In my case, I wish to reveal a hidden UIView coming from the right side of the screen with a swipe gesture to the left and also hide it again through the same gesture (this time to the right).

I've managed to get a solution on my previous post regarding showing/hiding a UIView. One thing I wish to add is the swiping gesture.

I wouldn't want to tweak my app delegate to do this as Nick Harris has done. So if anyone has any ideas/code samples on how I could do this, I'd be much grateful.

Shed some light :)

回答1:

You can reveal a hidden UIView coming from the right side of the screen with a swipe gesture to the left and also hide it again through the same gesture (this time to the right). adding transition.

Add BOOL didNotSwipe in .h file add its value didNotSwipe = TRUE in .m file viewDidLoad method

Add swipe gesture with left and right direction with different selector to your self.view.

 UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer:recognizer];
[recognizer release]; 

UISwipeGestureRecognizer *recognizer1;
recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeleft:)];
[recognizer1 setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:recognizer1];
[recognizer1 release]; 

When swipe left this method is called:

 -(void)swipeleft:(UISwipeGestureRecognizer *)swipeGesture 
 {
   if (didNotSwipe) {
     didNotSwipe = FALSE;
     CATransition *animation = [CATransition animation];
     [animation setDelegate:self];
     [animation setType:kCATransitionPush];
     [animation setSubtype:kCATransitionFromRight];
     [animation setDuration:0.50];
     [animation setTimingFunction:
     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
     [self.view.layer addAnimation:animation forKey:kCATransition];
     [self.view addSubView:self.overlayView];
     //[self.overlayView setFrame:CGRectMake(0,0,self.overlayView.frame.size.width,self.overlayView.frame.size.height)];
   }
 }

when swipe right this method:

 -(void)swipeRight:(UISwipeGestureRecognizer *)swipeGesture
 {
   if(!didNotSwipe){
     didNotSwipe = TRUE;
     CATransition *animation = [CATransition animation];
     [animation setDelegate:self];
     [animation setType:kCATransitionPush];
     [animation setSubtype:kCATransitionFromLeft];
     [animation setDuration:0.40];
     [animation setTimingFunction:
     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
     [self.view.layer addAnimation:animation forKey:kCATransition];
     [self.overlayView removeFromSuperView];
   }
 }