【发布时间】:2015-04-07 17:11:55
【问题描述】:
我想自定义推送和弹出视图控制器,使用下拉/上拉动画,如下所示:
我尝试更改 y 位置,但它不起作用(根本不显示动画)。
[self.navigationController pushViewController:nextController animated:NO];
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[UIView animateWithDuration:0.6 animations:^{
self.view.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
} completion:nil];
有什么建议吗?
P/s:在这种情况下我必须使用推送视图控制器而不是presentViewController
更新:
我尝试像这样使用 UINavigationControllerDelegate:
PropertyViewController.h
@interface PropertyViewController : UIViewController <UINavigationControllerDelegate>{
}
PropertyViewController.m
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
NSLog(@"willShowViewController");
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
NSLog(@"didShowViewController");
}
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
// THIS METHOD IS NOT CALLED AT ALL
NSLog(@"animationControllerForOperation");
TLTransitionAnimator *animator = [TLTransitionAnimator new];
animator.presenting = (operation == UINavigationControllerOperationPush);
animator.duration = 0.5;
return animator;
}
- (void)viewGallery{
// PUSH VIEW CONTROLLER
GalleryViewController* galleryController = [[GalleryViewController alloc] initWithNibName:@"GalleryViewController" bundle:nil];
galleryController.navigationController.delegate = self;
[self.navigationController pushViewController:galleryController animated:YES];
}
更新 2:
修复 PropertyViewController.m
self.navigationController.delegate = self;
我面临另一个问题。
push 时的向下滑动动画确实有效,但向上滑动无效。
这是我的自定义动画:
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
[[transitionContext containerView] addSubview:toViewController.view];
if (self.presenting) { // push
fromViewController.view.transform = CGAffineTransformIdentity;
toViewController.view.transform = CGAffineTransformMakeTranslation(0, toViewController.view.frame.size.height);
}else{ // pop
fromViewController.view.transform = CGAffineTransformMakeTranslation(0, fromViewController.view.frame.size.height);
}
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
if (self.presenting) { // push
fromViewController.view.transform = CGAffineTransformMakeTranslation(0, toViewController.view.frame.size.height);
toViewController.view.transform = CGAffineTransformMakeTranslation(0, toViewController.view.frame.size.height);
} else { // pop
fromViewController.view.transform = CGAffineTransformMakeTranslation(0, 0);
}
} completion:^(BOOL finished) {
toViewController.view.transform = CGAffineTransformIdentity;
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
问题2:
下拉动画只在第一次起作用,从第二次开始,布局呈现错误,我不知道为什么会发生这种情况。
我哪里错了?
【问题讨论】:
标签: ios objective-c uiviewcontroller