#import
<UIKit/UIKit.h>
@interface
ViewController :
UIViewController
@end
————————————————————————————————————————————————————————————————————————————————————————————
#import
"ViewController.h"
#import
"TestViewController.h"
@interface
ViewController ()
@end
@implementation
ViewController
-
(
void
)viewDidLoad
{
[
super
viewDidLoad];
self
.view.backgroundColor
= [
UIColor
brownColor];
UIButton
*btn = [[
UIButton
alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
btn.backgroundColor
= [
UIColor
yellowColor];
[btn
addTarget:
self
action:
@selector
(btnnnn)
forControlEvents:
UIControlEventTouchUpInside
];
[
self
.view
addSubview:btn];
}
-
(
void
)btnnnn{
TestViewController
*testVC = [[TestViewController alloc] init];
[
self
presentViewController:testVC animated:
NO
completion:^{}];
}
-
(
void
)didReceiveMemoryWarning
{
[
super
didReceiveMemoryWarning];
}
@end
————————————————————————————————————————————————————————————————————————————————————————————
第二个视图MyPresentationConytollrt继承
UIPresentationController
完成视图改变的代码就在这里面面面
————————————————————————————————————————————————————————————————————————————————————————————
#import
<UIKit/UIKit.h>
@interface
MyPresentationConytollrt :
UIPresentationController
@property
(
nonatomic
,
strong)
UIView
*dimmingView;
@property
(
nonatomic
,strong)
void
(^hidssView)();
@end
————————————————————————————————————————————————————————————————————————————————————————————
#import
"MyPresentationConytollrt.h"
@interface
MyPresentationConytollrt ()
@end
@implementation
MyPresentationConytollrt
-
(instancetype)initWithPresentedViewController:(
UIViewController
*)presentedViewController presentingViewController:(
UIViewController
*)presentingViewController
{
if
((
self
= [
super
initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController])) {
_dimmingView
= [[
UIView
alloc] init];
_dimmingView.backgroundColor
= [
UIColor
clearColor];
_dimmingView.alpha
= 0;
UITapGestureRecognizer
*hidssTouch = [[
UITapGestureRecognizer
alloc]initWithTarget:
self
action:
@selector
(hidssViewTouch)];
[_dimmingView
addGestureRecognizer:hidssTouch];
}
return
self
;
}
-(
void
)hidssViewTouch{
if
(
self
.hidssView)
{
self
.hidssView();
}
}
-
(
void
)presentationTransitionWillBegin
{
_dimmingView.frame
=
self
.containerView.bounds;
[
self
.containerView
addSubview:_dimmingView];
[
self
.containerView
addSubview:
self
.presentedView];
id
<
UIViewControllerTransitionCoordinator
>
coordinator =
self
.presentingViewController.transitionCoordinator;
[coordinator
animateAlongsideTransition:^(
id
<
UIViewControllerTransitionCoordinatorContext
>
_Nonnull context) {
_dimmingView.alpha
= 1;
}
completion:^(
id
<
UIViewControllerTransitionCoordinatorContext
>
_Nonnull context) {
}];
}
-
(
void
)presentationTransitionDidEnd:(
BOOL
)completed
{
if
(!completed) {
[_dimmingView
removeFromSuperview];
}
}
-
(
void
)dismissalTransitionWillBegin
{
id
<
UIViewControllerTransitionCoordinator
>
coordinator =
self
.presentingViewController.transitionCoordinator;
[coordinator
animateAlongsideTransition:^(
id
<
UIViewControllerTransitionCoordinatorContext
>
_Nonnull context) {
_dimmingView.alpha
= 0.0;
}
completion:^(
id
<
UIViewControllerTransitionCoordinatorContext
>
_Nonnull context) {
}];
}
-
(
void
)dismissalTransitionDidEnd:(
BOOL
)completed
{
if
(completed) {
[_dimmingView
removeFromSuperview];
}
}
-
(CGRect)frameOfPresentedViewInContainerView
{
CGRect
frame =
self
.containerView.bounds;
frame.origin.x
= CGRectGetWidth([
UIScreen
mainScreen].bounds);
frame
= CGRectInset(frame, 0, 0);
return
frame;
}
@end
————————————————————————————————————————————————————————————————————————————————————————————
第三个视图TestViewController
弹出的视图
————————————————————————————————————————————————————————————————————————————————————————————
#import
<UIKit/UIKit.h>
@interface
TestViewController :
UIViewController
<
UIViewControllerTransitioningDelegate
>
@end
————————————————————————————————————————————————————————————————————————————————————————————
#import
"TestViewController.h"
#import
"MyPresentationConytollrt.h"
@interface
TestViewController ()
@end
@implementation
TestViewController
-
(instancetype)init
{
if
((
self
= [
super
init])) {
self
.modalPresentationStyle
=
UIModalPresentationCustom
;
self
.transitioningDelegate
=
self
;
}
return
self
;
}
-
(
void
)viewDidLoad
{
[
super
viewDidLoad];
self
.view.backgroundColor
= [
UIColor
darkGrayColor];
UIButton
*close = [
UIButton
buttonWithType:
UIButtonTypeCustom
];
close.frame
= CGRectMake(0, 0, 100, 30);
close.center
=
self
.view.center;
[close
setTitle:@
"退出"
forState:
UIControlStateNormal
];
[close
addTarget:
self
action:
@selector
(closeAction:)
forControlEvents:
UIControlEventTouchUpInside
];
[
self
.view
addSubview:close];
}
-
(
void
)viewDidAppear:(
BOOL
)animated
{
[
super
viewDidAppear:animated];
[
UIView
animateWithDuration:0.2 animations:^{
CGRect
rect = CGRectMake(50, 0, CGRectGetWidth([
UIScreen
mainScreen].bounds) - 50, CGRectGetHeight([
UIScreen
mainScreen].bounds));
self
.view.frame
= rect;
}];
}
-
(
void
)closeAction:(
UIButton
*)sender
{
[
UIView
animateWithDuration:0.2 animations:^{
CGRect
rect = CGRectMake(CGRectGetWidth([
UIScreen
mainScreen].bounds), 0, CGRectGetWidth([
UIScreen
mainScreen].bounds), CGRectGetHeight([
UIScreen
mainScreen].bounds));
self
.view.frame
= rect;
}
completion:^(
BOOL
finished) {
[
self
dismissViewControllerAnimated:
NO
completion:^{
}];
}];
}
-
(
UIPresentationController
*)presentationControllerForPresentedViewController:(
UIViewController
*)presented presentingViewController:(
UIViewController
*)presenting sourceViewController:(
UIViewController
*)source
{
if
(presented ==
self
)
{
MyPresentationConytollrt
*presen = [[MyPresentationConytollrt alloc] initWithPresentedViewController:presented presentingViewController:presenting];
[presen
setHidssView:^{
[
UIView
animateWithDuration:0.2 animations:^{
CGRect
rect = CGRectMake(CGRectGetWidth([
UIScreen
mainScreen].bounds), 0, CGRectGetWidth([
UIScreen
mainScreen].bounds), CGRectGetHeight([
UIScreen
mainScreen].bounds));
self
.view.frame
= rect;
}
completion:^(
BOOL
finished) {
[
self
dismissViewControllerAnimated:
NO
completion:^{
}];
}];
}];
return
presen;
}
return
nil
;
}
-
(
void
)didReceiveMemoryWarning
{
[
super
didReceiveMemoryWarning];
}
@end