我在UViewController
内部玩弄新UIViewControllerContextTransitioning
UINavigationViewController
我所拥有的和我想要实现的目标很简单,但我在某个地方失败了
我在UIScrollView
内有一个MKMapView
(300px * 60px)。通过单击该地图(无论它在可见滚动中的哪个位置),我想转到包含全尺寸MKMapView
的下一个视图。我的目标是让用户感觉他们被吸进了地图(有点)=)
到目前为止,我已经添加了: <UIViewControllerTransitioningDelegate, UINavigationControllerDelegate>
和
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
if (operation == UINavigationControllerOperationPush) {
NSLog(@"inside itself");
FixRTransitionAnimator *animator = [FixRTransitionAnimator new];
animator.presenting = YES;
CGRect absoluteframe = [self.mainScrollView convertRect:self.mapView.frame toView:self.view];
animator.mapFrame = absoluteframe;
NSLog(@"the map frame is %@", NSStringFromCGRect(self.mapView.frame));
return animator;
}
else
NSLog(@"nope its outside");
return nil;
}
对于我拥有的prepareForSegue
方法:
[super prepareForSegue:sender sender:sender];
MapViewController *mapViewController = segue.destinationViewController;
mapViewController.transitioningDelegate = self;
mapViewController.modalPresentationStyle = UIModalPresentationCustom;
最后,我有了"FixRTransitionAnimator.m"
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
NSLog(@"the time is timed");
return 0.5f;
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
NSLog(@"we are inside %@",NSStringFromCGRect(self.mapFrame));
UIViewController *fromVController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
CGRect endFrame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height);
if (self.presenting) {
fromVController.view.userInteractionEnabled = NO;
[[transitionContext containerView]addSubview:fromVController.view];
[[transitionContext containerView]addSubview:toVController.view];
toVController.view.frame = self.mapFrame;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
fromVController.view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
NSLog(@"inside the animation");
toVController.view.frame = endFrame;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}else{
...
}
}
过渡动画在目的地视图控制器为空的情况下工作得很好。但是动画中的地图真的很奇怪,根本不像我想要的那样。
Any1 有任何指示,说明我想念或需要考虑什么?
我在另一个答案中写了一篇文章,但请确保您正在设置UINavigationControllerDelegate。
呈现的控制器有一个UIViewControllerTransitioningDelegate
,推送控制器有一个UINavigationControllerDelegate
。 协议中的委托方法返回动画器。