通过滑动滑出导航以便UIView覆盖另一个UIView

时间:2012-08-22 04:10:13

标签: objective-c ios uiview uiviewanimation

关于这种导航技术的研究,我发现this post by Nick Harris这是一个很好的开始,我想。但是,我想要的是与此略有不同。当您只需从顶部滑动以显示Notification Center时,您可以将其视为iOS“view,然后向后滑动以再次隐藏它。在我的情况下,我希望通过向左滑动手势从屏幕右侧显示隐藏的UIView,并通过相同的手势再次隐藏它(这次是向右)。

我设法在我之前的post上找到关于显示/隐藏UIView的解决方案。我想补充一点的是滑动手势。

我不想像尼克哈里斯那样调整我的应用代表来做这件事。因此,如果有人对我如何做到这一点有任何想法/代码样本,我将非常感激。

棚了一些光:)

1 个答案:

答案 0 :(得分:3)

您可以通过向左滑动手势向屏幕右侧显示隐藏的UIView,并通过相同的手势再次隐藏它(此时向右)。添加过渡。

在.h文件中添加BOOL didNotSwipe在.m文件中添加其值didNotSwipe = TRUE viewDidLoad方法

使用不同的选择器向左右方向添加滑动手势到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]; 

向左滑动时,会调用此方法:

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

向右滑动此方法时:

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