我正在尝试进行一个非常简单的过渡:一个视图将屏幕的一半移动到左侧,而第二个(“到”)视图移动半个屏幕.
我有动画工作,但当我反转动画时,我看到一个闪烁. “to”视图(即原始视图)在0,0的原点可见,尽管我设置了不同的帧.
我转储了视图层次结构.帧设置正确(-100 0; 320 480用于查看),但是它显示为0,0.视图的屏幕截图是否缓存在动画的某处?
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext { UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIView *container = [transitionContext containerView]; CGRect offsetCoverRect = CGRectMake(-100.0,0.0,320,480); CGRect detailsRect = CGRectMake(-100.0 + 320.0,480); CGRect detailsOutsideRect = CGRectMake(320.0,480); CGRect normalRect = CGRectMake(0.0,480); if (self.revealDetails) { toViewController.view.frame = detailsOutsideRect; [container addSubview:toViewController.view]; } else { [container insertSubview:toViewController.view belowSubview:fromViewController.view]; // reversing… set the frame to the original offset (shows at 0,0 for a moment) toViewController.view.frame = offsetCoverRect; } [UIView animateKeyframesWithDuration:2 delay:0 options:0 animations:^{ if (self.revealDetails) { fromViewController.view.frame = offsetCoverRect; toViewController.view.frame = detailsRect; } else { fromViewController.view.frame = detailsOutsideRect; toViewController.view.frame = normalRect; } } completion:^(BOOL finished) { [transitionContext completeTransition:finished]; }]; }
更新:
它似乎与UIModalPresentationCustom有关.我需要使用它,以便在转换完成时不删除from视图.但是,它似乎假设反向转换的from视图控制器从0,0开始.
更新2:
使用以下代码非常容易重现:
UIView *snapshot = [containerView snapshotViewAfterScreenUpdates:NO]; [containerView addSubview:snapshot];
以上将显示以屏幕为中心的视图,无论我在动画之前设置的实际帧或中心.