IOS - Create Push Segue Animation Without UINavigationController

时间:2020-12-01 17:27:56

APPLE提供了三种storyboard segue的方式:push,modal,custom .

push segue是系统预定义的跳转方式,

IOS - Create Push Segue Animation Without UINavigationController

为了使其能正常工作,我们还必须加载UINavigationController。

有时候,我们不想看到UINavigation bar,我们可以使用modal segue。

modal segue 的跳转方式有四种:Cover Vertical, Flip Horizontal, Cross Dissolve and Partial Curl。

要是我们想要的跳转方式与这四种方式都不同,我们可以使用自定义跳转方式custom segue。

下面是一个实现custom segue的样例:

1.创建一个UIStoryboardsegue的子类

IOS - Create Push Segue Animation Without UINavigationController

2.重载-(void)perform 方法

 1 - (void) perform
 2 {
 3     UIViewController *desViewController = (UIViewController *)self.destinationViewController;
 4     
 5     UIView *srcView = [(UIViewController *)self.sourceViewController view];
 6     UIView *desView = [desViewController view];
 7     
 8     desView.transform = srcView.transform;
 9     desView.bounds = srcView.bounds;
     
     if(isLandscapeOrientation)
     {
         if(isDismiss)
         {
             desView.center = CGPointMake(srcView.center.x, srcView.center.y  - srcView.frame.size.height);
         }
         else
         {
             desView.center = CGPointMake(srcView.center.x, srcView.center.y  + srcView.frame.size.height);
         }
     }
     else
     {
         if(isDismiss)
         {
             desView.center = CGPointMake(srcView.center.x - srcView.frame.size.width, srcView.center.y);
         }
         else
         {
             desView.center = CGPointMake(srcView.center.x + srcView.frame.size.width, srcView.center.y);
         }
     }
     
     
     UIWindow *mainWindow = [[UIApplication sharedApplication].windows objectAtIndex:];
     [mainWindow addSubview:desView];
     
     // slide newView over oldView, then remove oldView
     [UIView animateWithDuration:0.3
                      animations:^{
                          desView.center = CGPointMake(srcView.center.x, srcView.center.y);
                          
                          if(isLandscapeOrientation)
                          {
                              if(isDismiss)
                              {
                                  srcView.center = CGPointMake(srcView.center.x, srcView.center.y + srcView.frame.size.height);
                              }
                              else
                              {
                                  srcView.center = CGPointMake(srcView.center.x, srcView.center.y - srcView.frame.size.height);
                              }
                          }
                          else
                          {
                              if(isDismiss)
                              {
                                  srcView.center = CGPointMake(srcView.center.x + srcView.frame.size.width, srcView.center.y);
                              }
                              else
                              {
                                  srcView.center = CGPointMake(srcView.center.x - srcView.frame.size.width, srcView.center.y);
                              }
                          }
                      }
                      completion:^(BOOL finished){
                          //[desView removeFromSuperview];
                          [self.sourceViewController presentModalViewController:desViewController animated:NO];
                      }];

70 }

在viewcontroller中,重载prepareforsegue方法

 1 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 2 {
 3     HorizontalSlideSegue *s = (HorizontalSlideSegue *)segue;
 4     s.isDismiss = NO;
 5     
 6     if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
 7     {
 8         s.isLandscapeOrientation = YES;
 9     }
     else
     {
         s.isLandscapeOrientation = NO;
     }

14 }

3.选择custom segue 设置segue class为:customsegue(我们自定义的类)

IOS - Create Push Segue Animation Without UINavigationController

4.使用代码方式调用segue:

IOS - Create Push Segue Animation Without UINavigationController