1. [代码]iOS关于UIPresentationController 在视图上面创建一个UIViewController并改变大
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
六个文件 传不上去 就一次传两个吧
——————————————————————————————————————————————————————————————————————————————————————————
第一个视图 主视图ViewController
——————————————————————————————————————————————————————————————————————————————————————————
#import <UIKit/UIKit.h>
@interface
ViewController :
UIViewController
@end
————————————————————————————————————————————————————————————————————————————————————————————
#import "ViewController.h"
#import "TestViewController.h"
@interface
ViewController ()
@end
@implementation
ViewController
- (
void
)viewDidLoad {
[
super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
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];
// Dispose of any resources that can be recreated.
}
@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];
}
}
//改变页面的frame
- (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];
// Do any additional setup after loading the view.
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];
// Dispose of any resources that can be recreated.
}
@end
|