4

NavigationController과 함께 앱을 가지고 있습니다. 애니메이션 전환 스타일을 pushViewControllerpopToViewController으로 변경하려면 어떻게해야합니까?애니메이션 전환 변경

UPD

나는 @lawicko 대답처럼 카테고리를 만들었습니다. 하지만 함수를 호출하려고 할 때 오류가 발생했습니다.

[self.navigationController pushViewController : places withCustomTransition : CustomViewAnimationTransitionPush subtype : CustomViewAnimationSubtypeFromLeft];

오류 : 나는이 부분 선언해야

" 'CustomViewAnimationTransitionPush'선언되지 않은 식별자의 사용"내가 UINavigationController+Additions.h

UPD 2를 선언 지금

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

쓰기 : 하나 더 새로운 오류 :

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

및 동일한 오류가 발생하면 모두 _kCATransitions

+0

시도해 보셨습니까? [1] : http://stackoverflow.com/questions/3699882/can-i-curl-down-a-page-on-popviewcontrolleranimated – Ganzolo

+0

괜찮아요.하지만 애니메이션은 뒤로 버튼과 비슷합니다.하지만 설정했습니다. 그것 pushViewController에 대한 –

+0

난 정말 이해가 안돼. 보기 (pushViewController)를 누를 때 뒤로 (popViewController)를 클릭 할 때 가지고있는 애니메이션을 원하십니까? – 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 

당신이 구현 파일에 넣어해야 할이 두 번째 부분 :

당신은 _OBJC_CLASS_$_CATransition 오류를 해결하기 위해 대상에 QuartzCore.framework를 추가 할 필요가
#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

. 나는 최근에 내 자신의 변화를 만드는 달려

0

, 여기에 내가 만든 재사용 가능한 라이브러리입니다 :

https://github.com/travisjeffery/TRVSNavigationControllerTransition

그리고 여기 내 blog post 자신의 전환을하는 방법에 대해 이야기합니다.

기본 아이디어는 매우 간단합니다. navigationController (현재) 뷰의 CALayer 스냅 샷을 찍은 다음 애니메이션없이 뷰를 푸시/팝하고 새 뷰의 CALayer 스냅 샷을 찍은 다음 자신 만의 애니메이션을 추가하십시오. 애니메이션이 완성되면 해당 레이어를 제거한 다음 해당 레이어를 제거합니다.

+0

스냅 샷을 사용하면 전환이 심각하게 지연됩니다. 특히 오래된 장치에서. – Alex1987

+0

iOS 7에는이 작업을 특히 빠르게 처리 할 수있는 방법이 있습니다. – travisjeffery