【发布时间】:2016-10-09 19:52:10
【问题描述】:
我已将自定义动画过渡添加到视图控制器 - 两者都显示和关闭,但它在 iPhone 7 模拟器 (Xcode 8) 中不起作用,但动画过渡在 iPhone 6s 和之前的模拟器中运行良好。
在 iPhone 7 Simulator 上运行时,过渡时会出现白色背景,然后显示下一个视图控制器。
不知道为什么会这样?有人知道为什么吗?
代码
#import "HEREINExpandAnimator.h"
#define kTransitionDuration 0.65
@interface HEREINExpandAnimator ()
{
UIView *topView;
UIView *bottomView;
}
@end
@implementation HEREINExpandAnimator
+ (id)animator {
static HEREINExpandAnimator *animator = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
animator = [[self alloc] init];
});
return animator;
}
#pragma mark - UIViewControllerAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return kTransitionDuration;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
// From VC
UIViewController *fromController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
CGRect fromViewFrame = fromController.view.frame;
// To VC
UIViewController *toController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// Container View
UIView* container = [transitionContext containerView];
if (self.transitionMode == Presented) {
//create snapshots
topView = [fromController.view resizableSnapshotViewFromRect:fromViewFrame afterScreenUpdates:YES withCapInsets:UIEdgeInsetsMake(self.openingFrame.origin.y, 0, 0, 0)];
topView.frame = CGRectMake(0, 0, fromViewFrame.size.width, self.openingFrame.origin.y);
//add our snapshots on top
[container addSubview:topView];
bottomView = [fromController.view resizableSnapshotViewFromRect:fromViewFrame afterScreenUpdates:YES withCapInsets:UIEdgeInsetsMake(0, 0, fromViewFrame.size.height - self.openingFrame.origin.y - self.openingFrame.size.height , 0)];
bottomView.frame = CGRectMake(0, self.openingFrame.origin.y + self.openingFrame.size.height, fromViewFrame.size.width, fromViewFrame.size.height - self.openingFrame.origin.y - self.openingFrame.size.height);
[container addSubview:bottomView];
// Take snapshot of the to viewcontroller and change the height to the opening frame.
UIView *snapshotView = [toController.view resizableSnapshotViewFromRect:fromViewFrame afterScreenUpdates:YES withCapInsets:UIEdgeInsetsZero];
snapshotView.frame = self.openingFrame;
[container addSubview:snapshotView];
toController.view.alpha = 0.0;
[self changeAlphaOfView:fromController.view withAlpha:0.0];
[container addSubview:toController.view];
[UIView animateWithDuration:kTransitionDuration delay:0 usingSpringWithDamping:0.85 initialSpringVelocity:1.5 options:UIViewAnimationOptionCurveEaseOut animations:^{
//adjust the new frames
topView.frame = CGRectMake(0, -topView.frame.size.height, topView.frame.size.width, topView.frame.size.height);
bottomView.frame = CGRectMake(0, fromViewFrame.size.height, bottomView.frame.size.width, bottomView.frame.size.height);
snapshotView.frame = toController.view.frame;
} completion:^(BOOL finished) {
//don't forget to clean up
[UIView animateWithDuration:0.33 animations:^{
[snapshotView removeFromSuperview];
}];
toController.view.alpha = 1.0;
[self changeAlphaOfView:fromController.view withAlpha:1.0];
[transitionContext completeTransition:finished];
}];
}
else {
UIView *snapshotView = [fromController.view resizableSnapshotViewFromRect:fromController.view.bounds afterScreenUpdates:YES withCapInsets:UIEdgeInsetsZero];
[container addSubview:snapshotView];
fromController.view.alpha = 0.0;
[self changeAlphaOfView:toController.view withAlpha:0.0];
[UIView animateWithDuration:0.55 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
topView.frame = CGRectMake(0, 0, topView.frame.size.width, topView.frame.size.height);
bottomView.frame = CGRectMake(0, fromController.view.frame.size.height - bottomView.frame.size.height, bottomView.frame.size.width, bottomView.frame.size.height);
snapshotView.frame = self.openingFrame;
} completion:^(BOOL finished) {
[snapshotView removeFromSuperview];
fromController.view.alpha = 1.0;
[self changeAlphaOfView:toController.view withAlpha:1.0];
[transitionContext completeTransition:finished];
}];
}
}
-(void)changeAlphaOfView:(UIView *)contentView withAlpha:(CGFloat)alpha {
contentView.backgroundColor = [UIColor whiteColor];
for (UIView *view in contentView.subviews) {
view.alpha = alpha;
}
}
【问题讨论】:
-
你能展示你的代码吗? (如果有很多,请在github上发布一个项目。)谢谢。
-
@matt 我已经提到了代码
标签: ios objective-c xcode animation ios10