私のコードではpresentViewControllerを使用して2番目のビューコントローラーを呼び出しています
[self presentViewController:secondController animated:YES completion:nil];
呼び出すときに、左から右へのアニメーションを表示する必要があります(navigationControllerなど)
NavigationControllerは使いたくありませんが、presentViewControllerのnavigationControllerに似たアニメーションが必要です...
ビューコントローラーを表示する前にこのコード行を追加します
secondController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
secondController.modalPresentationStyle = UIModalPresentationFullScreen;
// Take a look at this enum
typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
Swiftの場合
アニメーションの種類を定義する
enum UIModalTransitionStyle : Int {
case CoverVertical = 0
case FlipHorizontal
case CrossDissolve
case PartialCurl
}
使い方
let vc : PageHomeViewController = storyboard!.instantiateViewControllerWithIdentifier("PageHomeViewController") as! PageHomeViewController
vc.modalTransitionStyle = .FlipHorizontal
self.presentViewController(vc, animated: true, completion: nil)
UIViewControllerTransitioningDelegateを使用してUINavigationViewController Pushメソッドのようにアニメーションを「水平方向にカバー」を解決するという私の決定。
1.カスタム遷移を作成します。
ヘッダー
@interface CoverHorizontalTransition: NSObject<UIViewControllerAnimatedTransitioning>
@property (assign, nonatomic) BOOL dismiss;
@end
実装
@implementation CoverHorizontalTransition
- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController;
fromViewController =
[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController;
toViewController =
[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *containerView = transitionContext.containerView;
CGRect animatedViewFrame;
animatedViewFrame = containerView.bounds;
animatedViewFrame.Origin = CGPointMake(CGRectGetWidth(animatedViewFrame), 0);
[containerView addSubview:toViewController.view];
if (_dismiss) {
[containerView bringSubviewToFront:fromViewController.view];
[UIView
animateWithDuration:[self transitionDuration:transitionContext]
animations:^{
fromViewController.view.frame = animatedViewFrame;
} completion:^(BOOL finished) {
[containerView.superview addSubview:toViewController.view];
[fromViewController.view removeFromSuperview];
[transitionContext completeTransition:YES];
}];
} else {
toViewController.view.frame = animatedViewFrame;
[UIView
animateWithDuration:[self transitionDuration:transitionContext]
animations:^{
toViewController.view.center = containerView.center;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
}
- (NSTimeInterval)transitionDuration:(nullable id<UIViewControllerContextTransitioning>)transitionContext
{
return 0.25;
}
@end
2.遷移デリゲートを作成します。
@implementation CustomViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
return [CoverHorizontalTransition new];
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
CoverHorizontalTransition *transition;
transition = [CoverHorizontalTransition new];
transition.dismiss = YES;
return transition;
}
@end
使用例。
...
// Save delegate to strong property
secondController.customTransitioningDelegate =
[BaseViewControllerTransitioningDelegate new];
secondController.transitioningDelegate =
secondController.customTransitioningDelegate;
secondController.modalPresentationStyle = UIModalPresentationCustom;
[self presentViewController:secondController animated:YES completion:nil];
このコードはiOS 10以降で動作します。