iOS关于UIPresentationController 在视图上面创建一个UIViewController并改变大小

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>
 
@interfaceViewController : UIViewController
 
 
@end
————————————————————————————————————————————————————————————————————————————————————————————
#import "ViewController.h"
#import "TestViewController.h"
 
@interfaceViewController ()
 
@end
 
@implementationViewController
 
- (void)viewDidLoad {
    [superviewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor  = [UIColorbrownColor];
    UIButton*btn = [[UIButtonalloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    btn.backgroundColor = [UIColoryellowColor];
    [btn addTarget:selfaction:@selector(btnnnn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}
- (void)btnnnn{
    TestViewController *testVC = [[TestViewController alloc] init];
    [selfpresentViewController:testVC animated:NOcompletion:^{}];
}
 
- (void)didReceiveMemoryWarning {
    [superdidReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
————————————————————————————————————————————————————————————————————————————————————————————
第二个视图MyPresentationConytollrt继承UIPresentationController 完成视图改变的代码就在这里面面面
————————————————————————————————————————————————————————————————————————————————————————————
#import <UIKit/UIKit.h>
 
@interfaceMyPresentationConytollrt : UIPresentationController
@property(nonatomic, strong) UIView*dimmingView;
@property(nonatomic,strong)void(^hidssView)();
@end
————————————————————————————————————————————————————————————————————————————————————————————
 
#import "MyPresentationConytollrt.h"
 
@interfaceMyPresentationConytollrt ()
 
@end
 
@implementationMyPresentationConytollrt
 
- (instancetype)initWithPresentedViewController:(UIViewController*)presentedViewController presentingViewController:(UIViewController*)presentingViewController
{
    if((self= [superinitWithPresentedViewController:presentedViewController presentingViewController:presentingViewController])) {
        _dimmingView = [[UIViewalloc] init];
        _dimmingView.backgroundColor = [UIColorclearColor];
        _dimmingView.alpha = 0;
        UITapGestureRecognizer*hidssTouch = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(hidssViewTouch)];
        [_dimmingView addGestureRecognizer:hidssTouch];
    }
    returnself;
}
 
//点击透明部分视图消失时间
-(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([UIScreenmainScreen].bounds);
    frame = CGRectInset(frame, 0, 0);
    returnframe;
}
 
@end
————————————————————————————————————————————————————————————————————————————————————————————
第三个视图TestViewController 弹出的视图
————————————————————————————————————————————————————————————————————————————————————————————
#import <UIKit/UIKit.h>
 
@interfaceTestViewController : UIViewController<UIViewControllerTransitioningDelegate>
 
@end
————————————————————————————————————————————————————————————————————————————————————————————
 
#import "TestViewController.h"
#import "MyPresentationConytollrt.h"
 
@interfaceTestViewController ()
 
@end
 
@implementationTestViewController
- (instancetype)init
{
    if((self= [superinit])) {
        self.modalPresentationStyle = UIModalPresentationCustom;
        self.transitioningDelegate = self;
    }
    returnself;
}
 
 
- (void)viewDidLoad {
    [superviewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColordarkGrayColor];
     
    UIButton*close = [UIButtonbuttonWithType:UIButtonTypeCustom];
    close.frame = CGRectMake(0, 0, 100, 30);
    close.center = self.view.center;
    [close setTitle:@"退出"forState:UIControlStateNormal];
    [close addTarget:selfaction:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:close];
}
 
- (void)viewDidAppear:(BOOL)animated
{
    [superviewDidAppear:animated];
    [UIViewanimateWithDuration:0.2 animations:^{
        CGRect rect = CGRectMake(50, 0, CGRectGetWidth([UIScreenmainScreen].bounds) - 50, CGRectGetHeight([UIScreenmainScreen].bounds));
        self.view.frame = rect;
    }];
}
 
- (void)closeAction:(UIButton*)sender
{
    [UIViewanimateWithDuration:0.2 animations:^{
        CGRect rect = CGRectMake(CGRectGetWidth([UIScreenmainScreen].bounds), 0, CGRectGetWidth([UIScreenmainScreen].bounds), CGRectGetHeight([UIScreenmainScreen].bounds));
        self.view.frame = rect;
    } completion:^(BOOLfinished) {
        [selfdismissViewControllerAnimated:NOcompletion:^{
        }];
    }];
     
}
 
- (UIPresentationController*)presentationControllerForPresentedViewController:(UIViewController*)presented presentingViewController:(UIViewController*)presenting sourceViewController:(UIViewController*)source
{
    if(presented == self) {
        MyPresentationConytollrt *presen = [[MyPresentationConytollrt alloc] initWithPresentedViewController:presented presentingViewController:presenting];
        [presen setHidssView:^{
            [UIViewanimateWithDuration:0.2 animations:^{
                CGRect rect = CGRectMake(CGRectGetWidth([UIScreenmainScreen].bounds), 0, CGRectGetWidth([UIScreenmainScreen].bounds), CGRectGetHeight([UIScreenmainScreen].bounds));
                self.view.frame = rect;
            } completion:^(BOOLfinished) {
                [selfdismissViewControllerAnimated:NOcompletion:^{
                     
                }];
            }];
        }];
        returnpresen;
    }
    returnnil;
}
 
- (void)didReceiveMemoryWarning {
    [superdidReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
@end

2. [图片] 屏幕快照 2016-01-28 15.12.07.png    

3. [图片] 屏幕快照 2016-01-28 15.12.16.png    

展开阅读全文

没有更多推荐了,返回首页