4

У меня есть приложение с NavigationController. Как я могу изменить стиль перехода анимации pushViewController и popToViewController?Изменение перехода анимации

UPD

Я создал категорию, как в @lawicko ответ. Но у меня возникла ошибка, когда я пытаюсь позвонить функции

[self.navigationController pushViewController: places withCustomTransition: CustomViewAnimationTransitionPush подтип: CustomViewAnimationSubtypeFromLeft];

ошибка: "Использование необъявленной идентификатора" CustomViewAnimationTransitionPush"

Где я должен объявить эту часть:

typedef enum { 
    CustomViewAnimationTransitionNone, 
    CustomViewAnimationTransitionFlipFromLeft, 
    CustomViewAnimationTransitionFlipFromRight, 
    CustomViewAnimationTransitionCurlUp, 
    CustomViewAnimationTransitionCurlDown, 
    CustomViewAnimationTransitionFadeIn, 
    CustomViewAnimationTransitionMoveIn, 
    CustomViewAnimationTransitionPush, 
    CustomViewAnimationTransitionReveal 
} CustomViewAnimationTransition; 

Записать сейчас я объявляю его в UINavigationController+Additions.h

UPD 2: Еще один новая ошибка:

Undefined symbols for architecture i386: 
    "_OBJC_CLASS_$_CATransition", referenced from: 
     objc-class-ref in UINavigationController+Additions.o 
    "_kCATransition", referenced from: 

и те же ошибки foor all _kCATпереходы

+0

Вы попробовали это? [1]: http://stackoverflow.com/questions/3699882/can-i-curl-down-a-page-on-popviewcontrolleranimated – Ganzolo

+0

thats ok, но мне нужна анимация, как в задней кнопке .. но установить это для pushViewController –

+0

Я действительно не понимаю. Вы хотите, чтобы анимация у вас была, когда вы нажимаете назад (popViewController), чтобы быть той, когда вы нажимаете вид (pushViewController)? – Ganzolo

ответ

19

Отметьте эту UINavigationController категорию, которую я создал. Это позволяет вам выталкивать и выскакивать практически во всех возможных переходах, а также поддерживает подтипы для переходов QuartzCore, которые позволят вам делать именно то, что вы хотите - нажмите вид слева. Сделайте это следующим образом:

[self.navigationController pushViewController:[[MyController alloc] init] withCustomTransition:CustomViewAnimationTransitionPush subtype:CustomViewAnimationSubtypeFromLeft]; 

Код приведен ниже. Первая часть, которую нужно поместить в части заголовка:

// IMPORTANT - basic transitions like flip and curl are local, they reside only in animation block. Core animations however, 
// once assigned to the layer, stay until changed or reset (by assigning nil as layer animation property) 

#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 
#import <QuartzCore/QuartzCore.h> 

typedef enum { 
    CustomViewAnimationTransitionNone, 
    CustomViewAnimationTransitionFlipFromLeft, 
    CustomViewAnimationTransitionFlipFromRight, 
    CustomViewAnimationTransitionCurlUp, 
    CustomViewAnimationTransitionCurlDown, 
    CustomViewAnimationTransitionFadeIn, 
    CustomViewAnimationTransitionMoveIn, 
    CustomViewAnimationTransitionPush, 
    CustomViewAnimationTransitionReveal 
} CustomViewAnimationTransition; 

#define CustomViewAnimationSubtypeFromRight kCATransitionFromRight 
#define CustomViewAnimationSubtypeFromLeft kCATransitionFromLeft 
#define CustomViewAnimationSubtypeFromTop kCATransitionFromTop 
#define CustomViewAnimationSubtypeFromBottom kCATransitionFromBottom 

@interface UINavigationController(Additions) 

- (void)pushViewController:(UIViewController *)viewController withCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype; 

- (void)popViewControllerWithCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype; 
- (void)popToRootViewControllerWithCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype; 
- (void)popToViewController:(UIViewController *)viewController withCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype; 

@end 

Это вторая часть, которую нужно поместить в файл реализации:

#import "UINavigationController_Additions.h" 

@interface UINavigationController() 

- (void)standardAnimationWithController:(UIViewController*)viewController 
           duration:(NSTimeInterval)duration 
           options:(UIViewAnimationOptions)options 
           changesBlock:(void (^)(void))block; 
- (void)coreAnimationWithController:(UIViewController*)viewController 
           duration:(NSTimeInterval)duration 
           type:(NSString*)type 
           subtype:(NSString*)subtype 
           changesBlock:(void (^)(void))block; 
@end 

@implementation UINavigationController(Additions) 

#pragma mark - 
#pragma mark pushing 

- (void)pushViewController:(UIViewController *)viewController withCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype { 
    switch (transition) { 
     case CustomViewAnimationTransitionNone:{ 
      [self standardAnimationWithController:viewController duration:.5 options:UIViewAnimationOptionTransitionNone 
              changesBlock:^{ 
               [self pushViewController:viewController animated:NO]; 
              }]; 
      break;} 
     case CustomViewAnimationTransitionFlipFromLeft:{ 
      [self standardAnimationWithController:viewController duration:.5 options:UIViewAnimationOptionTransitionFlipFromLeft 
              changesBlock:^{ 
               [self pushViewController:viewController animated:NO]; 
              }]; 
      break;} 
     case CustomViewAnimationTransitionFlipFromRight:{ 
      [self standardAnimationWithController:viewController duration:.5 options:UIViewAnimationOptionTransitionFlipFromRight 
              changesBlock:^{ 
               [self pushViewController:viewController animated:NO]; 
              }]; 
      break;} 
     case CustomViewAnimationTransitionCurlUp:{ 
      [self standardAnimationWithController:viewController duration:.5 options:UIViewAnimationOptionTransitionCurlUp 
              changesBlock:^{ 
               [self pushViewController:viewController animated:NO]; 
              }]; 
      break;} 
     case CustomViewAnimationTransitionCurlDown:{ 
      [self standardAnimationWithController:viewController duration:.5 options:UIViewAnimationOptionTransitionCurlDown 
              changesBlock:^{ 
               [self pushViewController:viewController animated:NO]; 
              }]; 
      break;} 
     case CustomViewAnimationTransitionFadeIn:{ 
      [self coreAnimationWithController:viewController duration:.5 type:kCATransitionFade subtype:nil 
           changesBlock:^{ 
            [self pushViewController:viewController animated:NO]; 
           }]; 
      break;} 
     case CustomViewAnimationTransitionMoveIn:{ 
      [self coreAnimationWithController:viewController duration:.5 type:kCATransitionMoveIn subtype:subtype 
           changesBlock:^{ 
            [self pushViewController:viewController animated:NO]; 
           }]; 
      break;} 
     case CustomViewAnimationTransitionPush:{ 
      [self coreAnimationWithController:viewController duration:.5 type:kCATransitionPush subtype:subtype 
           changesBlock:^{ 
            [self pushViewController:viewController animated:NO]; 
           }]; 
      break;} 
     case CustomViewAnimationTransitionReveal:{ 
      [self coreAnimationWithController:viewController duration:.5 type:kCATransitionReveal subtype:subtype 
           changesBlock:^{ 
            [self pushViewController:viewController animated:NO]; 
           }]; 
      break;} 
     default:{ 
      break;} 
    } 
} 

#pragma mark - 
#pragma mark popping 

- (void)popViewControllerWithCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype { 
    switch (transition) { 
     case CustomViewAnimationTransitionNone:{ 
      [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionNone 
            changesBlock:^{ 
             [self popViewControllerAnimated:NO]; 
            }]; 
      break;} 
     case CustomViewAnimationTransitionFlipFromLeft:{ 
      [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionFlipFromLeft 
            changesBlock:^{ 
             [self popViewControllerAnimated:NO]; 
            }]; 
      break;} 
     case CustomViewAnimationTransitionFlipFromRight:{ 
      [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionFlipFromRight 
            changesBlock:^{ 
             [self popViewControllerAnimated:NO]; 
            }]; 
      break;} 
     case CustomViewAnimationTransitionCurlUp:{ 
      [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionCurlUp 
            changesBlock:^{ 
             [self popViewControllerAnimated:NO]; 
            }]; 
      break;} 
     case CustomViewAnimationTransitionCurlDown:{ 
      [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionCurlDown 
            changesBlock:^{ 
             [self popViewControllerAnimated:NO]; 
            }]; 
      break;} 
     case CustomViewAnimationTransitionFadeIn:{ 
      [self coreAnimationWithController:nil duration:.5 type:kCATransitionFade subtype:nil 
           changesBlock:^{ 
            [self popViewControllerAnimated:NO]; 
           }]; 
      break;} 
     case CustomViewAnimationTransitionMoveIn:{ 
      [self coreAnimationWithController:nil duration:.5 type:kCATransitionMoveIn subtype:subtype 
           changesBlock:^{ 
            [self popViewControllerAnimated:NO]; 
           }]; 
      break;} 
     case CustomViewAnimationTransitionPush:{ 
      [self coreAnimationWithController:nil duration:.5 type:kCATransitionPush subtype:subtype 
           changesBlock:^{ 
            [self popViewControllerAnimated:NO]; 
           }]; 
      break;} 
     case CustomViewAnimationTransitionReveal:{ 
      [self coreAnimationWithController:nil duration:.5 type:kCATransitionReveal subtype:subtype 
           changesBlock:^{ 
            [self popViewControllerAnimated:NO]; 
           }]; 
      break;} 
     default:{ 
      break;} 
    } 
} 

- (void)popToRootViewControllerWithCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype { 
    switch (transition) { 
     case CustomViewAnimationTransitionNone:{ 
      [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionNone 
            changesBlock:^{ 
             [self popToRootViewControllerAnimated:NO]; 
            }]; 
      break;} 
     case CustomViewAnimationTransitionFlipFromLeft:{ 
      [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionFlipFromLeft 
            changesBlock:^{ 
             [self popToRootViewControllerAnimated:NO]; 
            }]; 
      break;} 
     case CustomViewAnimationTransitionFlipFromRight:{ 
      [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionFlipFromRight 
            changesBlock:^{ 
             [self popToRootViewControllerAnimated:NO]; 
            }]; 
      break;} 
     case CustomViewAnimationTransitionCurlUp:{ 
      [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionCurlUp 
            changesBlock:^{ 
             [self popToRootViewControllerAnimated:NO]; 
            }]; 
      break;} 
     case CustomViewAnimationTransitionCurlDown:{ 
      [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionCurlDown 
            changesBlock:^{ 
             [self popToRootViewControllerAnimated:NO]; 
            }]; 
      break;} 
     case CustomViewAnimationTransitionFadeIn:{ 
      [self coreAnimationWithController:nil duration:.5 type:kCATransitionFade subtype:nil 
           changesBlock:^{ 
            [self popToRootViewControllerAnimated:NO]; 
           }]; 
      break;} 
     case CustomViewAnimationTransitionMoveIn:{ 
      [self coreAnimationWithController:nil duration:.5 type:kCATransitionMoveIn subtype:subtype 
           changesBlock:^{ 
            [self popToRootViewControllerAnimated:NO]; 
           }]; 
      break;} 
     case CustomViewAnimationTransitionPush:{ 
      [self coreAnimationWithController:nil duration:.5 type:kCATransitionPush subtype:subtype 
           changesBlock:^{ 
            [self popToRootViewControllerAnimated:NO]; 
           }]; 
      break;} 
     case CustomViewAnimationTransitionReveal:{ 
      [self coreAnimationWithController:nil duration:.5 type:kCATransitionReveal subtype:subtype 
           changesBlock:^{ 
            [self popToRootViewControllerAnimated:NO]; 
           }]; 
      break;} 
     default:{ 
      break;} 
    }  
} 

- (void)popToViewController:(UIViewController *)viewController withCustomTransition:(CustomViewAnimationTransition)transition subtype:(NSString*)subtype { 
    switch (transition) { 
     case CustomViewAnimationTransitionNone:{ 
      [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionNone 
            changesBlock:^{ 
             [self popToViewController:viewController animated:NO]; 
            }]; 
      break;} 
     case CustomViewAnimationTransitionFlipFromLeft:{ 
      [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionFlipFromLeft 
            changesBlock:^{ 
             [self popToViewController:viewController animated:NO]; 
            }]; 
      break;} 
     case CustomViewAnimationTransitionFlipFromRight:{ 
      [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionFlipFromRight 
            changesBlock:^{ 
             [self popToViewController:viewController animated:NO]; 
            }]; 
      break;} 
     case CustomViewAnimationTransitionCurlUp:{ 
      [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionCurlUp 
            changesBlock:^{ 
             [self popToViewController:viewController animated:NO]; 
            }]; 
      break;} 
     case CustomViewAnimationTransitionCurlDown:{ 
      [self standardAnimationWithController:nil duration:.5 options:UIViewAnimationOptionTransitionCurlDown 
            changesBlock:^{ 
             [self popToViewController:viewController animated:NO]; 
            }]; 
      break;} 
     case CustomViewAnimationTransitionFadeIn:{ 
      [self coreAnimationWithController:nil duration:.5 type:kCATransitionFade subtype:nil 
           changesBlock:^{ 
            [self popToViewController:viewController animated:NO]; 
           }]; 
      break;} 
     case CustomViewAnimationTransitionMoveIn:{ 
      [self coreAnimationWithController:nil duration:.5 type:kCATransitionMoveIn subtype:subtype 
           changesBlock:^{ 
            [self popToViewController:viewController animated:NO]; 
           }]; 
      break;} 
     case CustomViewAnimationTransitionPush:{ 
      [self coreAnimationWithController:nil duration:.5 type:kCATransitionPush subtype:subtype 
           changesBlock:^{ 
            [self popToViewController:viewController animated:NO]; 
           }]; 
      break;} 
     case CustomViewAnimationTransitionReveal:{ 
      [self coreAnimationWithController:nil duration:.5 type:kCATransitionReveal subtype:subtype 
           changesBlock:^{ 
            [self popToViewController:viewController animated:NO]; 
           }]; 
      break;} 
     default:{ 
      break;} 
    }   
} 

#pragma mark - 
#pragma mark private 

- (void)standardAnimationWithController:(UIViewController*)viewController 
           duration:(NSTimeInterval)duration 
           options:(UIViewAnimationOptions)options 
           changesBlock:(void (^)(void))block { 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:duration]; 
    [UIView transitionWithView:self.view duration:duration options:options animations:block completion:NULL]; 
    [UIView commitAnimations]; 
} 

- (void)coreAnimationWithController:(UIViewController*)viewController 
           duration:(NSTimeInterval)duration 
           type:(NSString*)type 
           subtype:(NSString*)subtype 
           changesBlock:(void (^)(void))block { 
    CATransition* trans = [CATransition animation]; 
    [trans setDuration:duration]; 
    [trans setType:type]; 
    [trans setSubtype:subtype]; 
    [self.view.layer addAnimation:trans forKey:kCATransition]; 
    block(); 
} 

@end 
+0

ой!должен ли я включить его в свой пользовательский NavigationController? –

+0

Нет, просто вставьте этот код в файлы категорий (.h для интерфейса, .m для реализации) и убедитесь, что они доступны в вашем проекте. Затем импортируйте файл заголовка везде, где вы хотите использовать метод категории. Если у вас возникли проблемы с пониманием того, как работает категория, см. [Здесь] (https://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html). – lawicko

+0

Я создаю категорию .. но есть одна ошибка. посмотрите upd –

2

Вам нужно добавить QuartzCore.framework к вашей цели, чтобы решить _OBJC_CLASS_$_CATransition ошибку ,

0

Я недавно решен созданием своего собственного перехода, вот многоразовые библиотеки я сделал:

https://github.com/travisjeffery/TRVSNavigationControllerTransition

А вот мой blog post говорить о том, как сделать свой собственный переход.

Основная идея довольно проста, просто сделайте снимок CALayer в представлении navigationController (текущем), затем нажмите/отпустите представление без анимации, сделайте снимок CALayer нового представления, а затем добавьте свои собственные анимации в эти слои, а затем удалите эти слои после завершения анимации.

+0

Выполнение моментального снимка серьезно задерживает переход. Особенно на старых устройствах. – Alex1987

+0

У iOS 7 есть способ сделать это особенно быстро ... – travisjeffery