IOS開發 - Create Push Segue Animation Without UINavigationController

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   os   strong   

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

push segue是系統預定義的跳轉方式,

為了使其能正常工作,我們還必須載入UINavigationController。

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

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

要是我們想要的跳轉方式與這四種方式都不同,我們可以使用自訂跳轉方式custom segue。

下面是一個實現custom segue的範例:

1.建立一個UIStoryboardsegue的子類

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(我們自訂的類)

4.使用代碼方式調用segue: 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.