动画 UINavigationController 的“后退”按钮
-
29-09-2020 - |
题
我在导航控制器的层次结构中的视图控制器上有一个自定义按钮,按下该按钮时,会弹出可见的视图控制器。
我想使用 UIView 的 transform
属性来动画关闭视图控制器。它有效,但如果我使用“popViewControllerAnimated:YES”,动画的默认左幻灯片仍然存在,尽管我的自定义转换也有效。
如果我设置 popViewControllerAnimated:NO
它根本不动画任何东西。
我也考虑过使用 CATransition
当我有时这有效 popViewControllerAnimated
设置 NO
, ,但是公共 API 中没有“缩放”效果,而且我不想使用私有效果。自定义滤镜也不适用于 iPhone,仅适用于 OS X。
所以我想我的问题是:
1)有没有办法删除默认过渡中的左侧幻灯片,但仍然使用自定义动画 transform
?
2)使用自定义过滤器的一些方法 CATransition
?
3) 如果我使用私有 API 来实现缩放效果,Apple 将我的应用程序扔进拒绝箱的可能性有多大?
有人有我忽略的解决方案吗?
解决方案
我做过类似你所描述的事情,即更改 UINavigationController 中视图的弹出和推送的默认动画。
这个想法是禁用对象的默认动画并将其替换为您自己的动画。我为 UINavigationController 创建了一个新类别,并使用了类似于下面的函数。
- (void) altAnimatePopViewControllerAnimated:(BOOL)animated
{
[CATransaction begin];
CATransition *transition;
transition = [CATransition animation];
transition.type = kCATransitionPush; // Use any animation type and subtype you like
transition.subtype = kCATransitionFromTop;
transition.duration = 0.3;
CATransition *fadeTrans = [CATransition animation];
fadeTrans.type = kCATransitionFade;
fadeTrans.duration = 0.3;
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
[[[[self.view subviews] objectAtIndex:0] layer] addAnimation:transition forKey:nil];
[[[[self.view subviews] objectAtIndex:1] layer] addAnimation:fadeTrans forKey:nil];
[self popViewControllerAnimated:YES];
[CATransaction commit];
}
使用只需使用代码
[self.navigationController altAnimatePopViewControllerAnimated:YES];
为了进行推送,只需创建另一个类似的函数并反转动画。
它并不完美,但它有效。当您选择不同的动画类型时,请使用组成导航栏/控制器的不同子视图以使其完美。
我花了相当多的时间才想出这个,所以明智地使用它:)
******* 编辑
尝试用这个替换推送转换(我自己没有尝试过):
CAKeyframeAnimation *scale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
scale.duration = duration;
scale.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:.5f],
[NSNumber numberWithFloat:1.2f],
[NSNumber numberWithFloat:.85f],
[NSNumber numberWithFloat:1.f],
nil];
这将弹出,即在达到合适的尺寸之前,视图会变大。使用数组中的值来控制动画的曲线。