有时候在跳转控制器的时候需要,跳转到当前控制器上方,显示一个可以看到下方原来控制器的半透明控制器。
如下图:
跳转方式可以修改这句代码来实现,激变,从下往上。或者也可以自定义一些动画。(已经被我注释掉的那些)
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
跳转的时候根据要跳转到哪一层控制器上方的情况,修改如下代码
[self.tabBarController presentViewController:controller animated:YES completion:nil] (这里是跳转到tabBar控制器的上方。)
第一个控制器代码
#import "ViewController.h"
#import "MKDialogController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// fade //交叉淡化过渡(不支持过渡方向)
// push //新视图把旧视图推出去
// moveIn //新视图移到旧视图上面
// reveal //将旧视图移开,显示下面的新视图
// cube //立方体翻滚效果
// oglFlip //上下左右翻转效果
// suckEffect //收缩效果,如一块布被抽走(不支持过渡方向)
// rippleEffect //滴水效果(不支持过渡方向)
// pageCurl //向上翻页效果
// pageUnCurl //向下翻页效果
// cameraIrisHollowOpen //相机镜头打开效果(不支持过渡方向)
// cameraIrisHollowClose //相机镜头关上效果(不支持过渡方向)
}
- (IBAction)btnClick:(UIButton *)sender {
MKDialogController *controller = [[MKDialogController alloc] init];
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
controller.providesPresentationContextTransitionStyle = YES;
controller.definesPresentationContext = YES;
controller.modalPresentationStyle = UIModalPresentationOverCurrentContext;
// CATransition *animation = [CATransition animation];
// animation.duration = 2.0;
// animation.timingFunction = UIViewAnimationCurveEaseInOut;
// animation.type = @"cameraIrisHollowOpen";
animation.subtype = kCATransitionFromLeft;
// [self.view.window.layer addAnimation:animation forKey:nil];
[self.tabBarController presentViewController:controller animated:YES completion:nil];
} else {
self.view.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:controller animated:NO completion:nil];
self.view.window.rootViewController.modalPresentationStyle = UIModalPresentationFullScreen;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
要跳转的控制器代码
#import "MKDialogController.h"
@interface MKDialogController ()
@end
@implementation MKDialogController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor clearColor];
UIView *alphaView = [[UIView alloc] initWithFrame:self.view.frame];
UIView *baseView = [[UIView alloc] initWithFrame:self.view.frame];
alphaView.backgroundColor = [UIColor clearColor];
baseView.backgroundColor = [UIColor blackColor];
baseView.alpha = 0.8;
[self.view addSubview:baseView];
[self.view addSubview:alphaView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(20, 178, 100, 60);
[button setTitle:@"点我返回" forState:UIControlStateNormal];
button.backgroundColor = [UIColor brownColor];
[alphaView addSubview:button];
[button addTarget:self action:@selector(dissmiss) forControlEvents:UIControlEventTouchUpInside];
}
- (void)dissmiss{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end