我的要求是,当我从屏幕右侧滑动时(视图控制器A),然后需要使用交互式转换推到下一个视图控制器(视图控制器B)。使用相同的机制也可以解除,当我从屏幕左侧滑动时(视图控制器B),它会使用交互式转换解除控制器。如何以正确的方式实现它。我已经使用交互式转换实现了解雇视图控制器,但无法使用交互式转换实现推送到视图控制器
#import "AMSimpleAnimatedDismissal.h"
@implementation AMSimpleAnimatedDismissal
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 1.5f;
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
[[transitionContext containerView]addSubview:toVC.view];
CGRect toFrame = toVC.view.frame;
CGRect initialFrame = fromVC.view.frame;
toVC.view.frame = CGRectMake(-320 ,0, CGRectGetWidth(toFrame) , CGRectGetHeight(toFrame));
CGRect finalFrame = CGRectMake(initialFrame.size.width, initialFrame.origin.y, initialFrame.size.width, initialFrame.size.height);
UIViewAnimationOptions opts = UIViewAnimationOptionCurveLinear;
[UIView animateWithDuration:1.0 delay:0 options:opts animations:^{
fromVC.view.frame = finalFrame;
toVC.view.frame = CGRectMake(0, 0, CGRectGetWidth(fromVC.view.frame), CGRectGetHeight(fromVC.view.frame));
} completion:^(BOOL finished) {
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
这是我的交互式转换部分
可以设置滑动手势的方向:当调用动作方法时,只需检查滑动的方向,如果方向是UISwipeGestureRecognizerDirectionRight
,则使用[self.navigationController popViewControllerAnimated:BOOL]
。您可能希望向AMSimpleAnimatedDismissal
添加一个"isPop
"BOOL属性,并让此类同时处理表示和驳回。
如果使用导航控制器进行交互式自定义转换,则必须:
为
UINavigationController
指定delegate
UINavigationControllerDelegate
必须在以下代理协议方法中指定动画控制器:- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
如果你想让推送是交互式的,你也必须用以下方法指定一个交互控制器:
- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
如果由手势发起,此方法通常应返回
UIPercentDrivenInteractiveTransition
,如果不是,则返回nil
。您必须在视图上添加手势识别器(例如
UISwipeFromEdgeGestureRecognizer
),该识别器将启动转换并更新上述interactionControllerForAnimationController
方法返回的UIPercentDrivenInteractiveTransition
,并在完成手势时完成或取消转换。