IOS - Create Push Segue Animation Without UINavigationController

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

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

push

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

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

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

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

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

1.创建一个UIStoryboardsegue的子类

customsegue

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;
10     
11     if(isLandscapeOrientation)
12     {
13         if(isDismiss)
14         {
15             desView.center = CGPointMake(srcView.center.x, srcView.center.y  - srcView.frame.size.height);
16         }
17         else
18         {
19             desView.center = CGPointMake(srcView.center.x, srcView.center.y  + srcView.frame.size.height);
20         }
21     }
22     else
23     {
24         if(isDismiss)
25         {
26             desView.center = CGPointMake(srcView.center.x - srcView.frame.size.width, srcView.center.y);
27         }
28         else
29         {
30             desView.center = CGPointMake(srcView.center.x + srcView.frame.size.width, srcView.center.y);
31         }
32     }
33     
34     
35     UIWindow *mainWindow = [[UIApplication sharedApplication].windows objectAtIndex:0];
36     [mainWindow addSubview:desView];
37     
38     // slide newView over oldView, then remove oldView
39     [UIView animateWithDuration:0.3
40                      animations:^{
41                          desView.center = CGPointMake(srcView.center.x, srcView.center.y);
42                          
43                          if(isLandscapeOrientation)
44                          {
45                              if(isDismiss)
46                              {
47                                  srcView.center = CGPointMake(srcView.center.x, srcView.center.y + srcView.frame.size.height);
48                              }
49                              else
50                              {
51                                  srcView.center = CGPointMake(srcView.center.x, srcView.center.y - srcView.frame.size.height);
52                              }
53                          }
54                          else
55                          {
56                              if(isDismiss)
57                              {
58                                  srcView.center = CGPointMake(srcView.center.x + srcView.frame.size.width, srcView.center.y);
59                              }
60                              else
61                              {
62                                  srcView.center = CGPointMake(srcView.center.x - srcView.frame.size.width, srcView.center.y);
63                              }
64                          }
65                      }
66                      completion:^(BOOL finished){
67                          //[desView removeFromSuperview];
68                          [self.sourceViewController presentModalViewController:desViewController animated:NO];
69                      }];

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     }
10     else
11     {
12         s.isLandscapeOrientation = NO;
13     }

14 } 

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

drfine

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

 

posted @ 2014-08-01 11:02  姜楠  阅读(1805)  评论(0编辑  收藏  举报