就我而言,我希望在新的视图控制器中有一个视图,当此页面打开时,该视图的背景具有50%的Alpha。 正常情况下,我可以使用模态呈现形式的segue,然后可以轻松地更改背景alpha以查看父视图内容。现在,由于某种原因,我必须使用显示(推送)类型的segue来执行此操作,并且我仍然需要更改背景Alpha才能查看父视图的内容。 有可能吗?
非常感谢
这是视图的一张屏幕截图,背面的背景应该清晰
使用iOS 7中引入的视图控制器过渡API实现此效果相对简单。
我们将使用常规的UIViewControllerTransitioningDelegate方法指定动画。首先,我们将创建一个'animator'对象,该对象将模仿标准的UINavigationController幻灯片动画。
// UIViewControllerAnimatedTransitioning代码
@interface PushPopAnimator : NSObject <UIViewControllerAnimatedTransitioning>
@property (assign,nonatomic,getter=isPresenting) BOOL presenting;
@end
@implementation PushPopAnimator
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
return 0.35;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *container = [transitionContext containerView];
UIView *animatingView = self.isPresenting ? toVC.view : fromVC.view;
CGRect offsetFrame = CGRectOffset(container.frame,CGRectGetWidth(container.frame),0);
CGRect finalFrame = self.isPresenting ? container.frame : offsetFrame;
if (self.isPresenting) {
animatingView.frame = offsetFrame;
[container addSubview:animatingView];
}
[UIView animateWithDuration:[self transitionDuration:transitionContext]
delay:0.0
usingSpringWithDamping:0.9
initialSpringVelocity:2.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
animatingView.frame = finalFrame;
} completion:^(BOOL finished) {
BOOL cancelled = [transitionContext transitionWasCancelled];
[transitionContext completeTransition:!cancelled];
if (self.isPresenting && toVC.navigationController) {
[container insertSubview:fromVC.view belowSubview:toVC.view];
} else if (fromVC.navigationController && cancelled) {
[container insertSubview:toVC.view belowSubview:fromVC.view];
}
}];
}
@end
在viewController类内部
- (IBAction)pushModal:(UIButton *)sender {
[self.navigationController pushViewController:[self modalController] animated:YES];
}
- (UIViewController *)modalController {
UIViewController *modal = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([ModalViewController class])];
modal.modalPresentationStyle = UIModalPresentationCustom;
modal.transitioningDelegate = self;
return modal;
}
#pragma mark - UIViewControllerTransitioningDelegate
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
presentingController:(UIViewController *)presenting
sourceController:(UIViewController *)source {
return [self pushPopAnimatorForPresentation:YES];
}
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
return [self pushPopAnimatorForPresentation:NO];
}
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC {
return [self pushPopAnimatorForPresentation:operation == UINavigationControllerOperationPush];
}
- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController {
return self.interactionController;
}