仿照“豌豆荚一览”做的iOS转场动画
豌豆荚一览的转场动画非常炫,尤其是点击一个新闻条目跳转到新闻正文的时候
于是我做了个类似的,放在了GitHub上
转场动画介绍一下主要实现:
从VC1(ViewController1)跳转到VC2的时候,主要是调用下面这个方法,这个方法有两个参数,一个是要显示的下一个ViewController,还有一个是,动画从那个方块开始(因为豌豆荚一览是从某个cell开始的动画)
- (void) animToNextViewController:(UIViewController*)viewController beginRect:(CGRect)beginFrame {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
UIView* shadowBack = [[UIView alloc]initWithFrame:window.bounds];
shadowBack.backgroundColor = [UIColor blackColor];
shadowBack.alpha = 0.7;
[window addSubview:shadowBack];
UIView* frontWhiteView = [[UIView alloc]initWithFrame:beginFrame];
frontWhiteView.backgroundColor = [UIColor whiteColor];
[window addSubview:frontWhiteView];
NSTimeInterval timeInterval = 0.5;
__weak __typeof(self) weakSelf = self;
[UIView animateWithDuration:timeInterval/5 animations:^{
frontWhiteView.frame = CGRectMake(0, screenHeight/2-18, screenWidth, 36);
} completion:^(BOOL finished) {
[UIView animateWithDuration:4*timeInterval/5 animations:^{
frontWhiteView.frame = CGRectMake(0, 0, screenWidth, screenHeight);
} completion:^(BOOL finished) {
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewController];
viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
nav.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[weakSelf presentViewController:nav animated:NO completion:^{
[shadowBack removeFromSuperview];
[frontWhiteView removeFromSuperview];
}];
// [self.navigationController pushViewController:viewController animated:NO];
// [shadowBack removeFromSuperview];
// [frontWhiteView removeFromSuperview];
}];
}];
[UIView animateWithDuration:timeInterval animations:^{
weakSelf.navigationController.view.transform = CGAffineTransformMakeScale(0.85, 0.85);
} completion:^(BOOL finished) {
weakSelf.navigationController.view.transform = CGAffineTransformIdentity;
}];
}
这里做了一些操作,首先在window上,覆盖了一个半透明的黑色遮罩,然后又在window上覆盖了动画开始的白色方块。
之后通过动画,缩小再放大这个白色方块到铺满全屏,动画完成以后,presentViewController到下一个视图,注意presentViewController是不再需要动画了,所以传入NO
在这个动画同时,还有一个动画,它与上面的动画总时间相同,整个动画缩小了当前整个视图的(self.navigationController.view)的大小,有一种下沉的感觉,在动画结束的时候,这个时候下一个ViewController也已经覆盖到了当前视图上,所以恢复当前视图的大小
这里有一个关键的地方:
viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
nav.modalPresentationStyle = UIModalPresentationOverCurrentContext;
这两句可以保证,下一个视图覆盖了当前视图的时候,当前视图依然在渲染(具体请Google)
另外,在推入下一个ViewController,我也告诉了下一个VC,他上一个视图的当前视图
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
SecondViewController *secondVC = [[SecondViewController alloc]init];
secondVC.previousNav = self.navigationController;//告诉下一个视图,它的上一个视图是谁
[self animToNextViewController:secondVC beginRect:CGRectMake(0, screenHeight/2-20, screenWidth, 40)];
}
这样设置是为了方便下一个视图dismiss的时候,依然有一个退回的动画
下面就说结束动画:
- (void)dismissSelf {
if (self.previousNav) {
self.previousNav.view.transform = CGAffineTransformMakeScale(0.85, 0.85);
[UIView animateWithDuration:0.5 animations:^{
self.previousNav.view.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
}];
[self dismissViewControllerAnimated:YES completion:nil];
}else{
[self dismissViewControllerAnimated:YES completion:nil];
}
}
结束动画很简单,如果上一个视图的属性不为空,就执行定制动画,否则就执行默认动画
如果可以显示定制动画,首先把上一个视图设置成缩小的样子,然后在动画里逐渐放大视图,知道原样大小,
这样就可以流畅完整的实现整个动画了
如果您有更好的实现,或者我这个实现哪里非常耗费性能,还请多多指出,多谢大家。