DWUploadInfoSetupViewController.m 8.45 KB
Newer Older
Han Ruisong's avatar
Han Ruisong committed
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
#import "DWUploadInfoSetupViewController.h"

@interface DWUploadInfoSetupViewController () <UITextFieldDelegate>

@property(nonatomic,strong)UITextField * userIdTextField;
@property(nonatomic,strong)UITextField * apiKeyTextField;
@property(nonatomic,strong)UITextField * titleTextField;
@property(nonatomic,strong)UITextField * tagTextField;
@property(nonatomic,strong)UITextView * descriptionTextView;

@property(nonatomic,strong)UIScrollView * bgScrollView;

@end

@implementation DWUploadInfoSetupViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShowNotification:) name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHideNotification:) name:UIKeyboardDidHideNotification object:nil];
    
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (CGRectGetMaxY(self.descriptionTextView.superview.frame) > self.bgScrollView.frame.size.height) {
        self.bgScrollView.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(self.descriptionTextView.superview.frame) + 20);
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - action
-(void)returnButtonAction
{
    if (_backBlock) {
        _backBlock(YES, nil, nil, nil, nil, nil);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)uploadAction:(UIBarButtonItem *)item
Han Ruisong's avatar
Han Ruisong committed
60
{
Han Ruisong's avatar
Han Ruisong committed
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
    if (self.userIdTextField.text.length == 0) {
        [@"请输入User ID" showAlert];
        return;
    }
    
    if (self.apiKeyTextField.text.length == 0) {
        [@"请输入API Key" showAlert];
        return;
    }
    
    if (self.titleTextField.text.length == 0) {
        [@"请输入视频标题" showAlert];
        return;
    }
    
    if (_backBlock) {
        self.backBlock(NO, self.userIdTextField.text, self.apiKeyTextField.text, self.titleTextField.text, self.tagTextField.text, self.descriptionTextView.text);
    }
    
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)tapEndEditAction
{
    [self.view endEditing:YES];
}

- (void)didBackBlock:(BackBlock )block{
    
    _backBlock = block;
}

-(void)keyboardDidShowNotification:(NSNotification *)noti
{
    NSDictionary *userInfo = [noti userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;
    
    UIView * firstResponderBgView = nil;
    if (self.descriptionTextView.isFirstResponder) {
        firstResponderBgView = self.descriptionTextView.superview;
    }else{
        for (int i = 0; i <= 3; i++) {
            UITextField * textField = (UITextField *)[self.bgScrollView viewWithTag:100 + i];
            if (textField.isFirstResponder) {
                firstResponderBgView = textField.superview;
                break;
            }
        }
    }
    CGRect frame = [firstResponderBgView convertRect:firstResponderBgView.bounds toView:self.bgScrollView];
    if (CGRectGetMaxY(frame) > CGRectGetMaxY(self.bgScrollView.frame) - height) {
        [UIView animateWithDuration:0.33 animations:^{
            self.bgScrollView.contentOffset = CGPointMake(0, height);
        }];
    }
}

-(void)keyboardDidHideNotification:(NSNotification *)noti
{
    [UIView animateWithDuration:0.33 animations:^{
        self.bgScrollView.contentOffset = CGPointMake(0, 0);
    }];
}

# pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark - init
-(void)initUI
{
    self.title = @"填写视频信息";
    
    UIBarButtonItem *uploadItem = [[UIBarButtonItem alloc] initWithTitle:@"确认" style:UIBarButtonItemStylePlain target:self action:@selector(uploadAction:)];
    
    self.navigationItem.rightBarButtonItem = uploadItem;
    
    self.bgScrollView = [[UIScrollView alloc]init];
    self.bgScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.bgScrollView];
    [_bgScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(@0);
        make.width.equalTo(self.view);
        make.height.equalTo(self.view);
    }];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapEndEditAction)];
    [self.bgScrollView addGestureRecognizer:tap];
    
    UILabel * tsLabel = [[UILabel alloc]init];
    tsLabel.text = @"温馨提示:演示账号没有开通上传视频的权限,如需测试上传功能,请填写自己的账号信息";
    tsLabel.font = TitleFont(16);
    tsLabel.numberOfLines = 0;
    tsLabel.textColor = [UIColor colorWithRed:255/255.0 green:146/255.0 blue:10/255.0 alpha:1.0];
    [self.bgScrollView addSubview:tsLabel];
    [tsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(@20);
        make.width.equalTo(@(ScreenWidth - 20));
        make.height.equalTo(@60);
    }];
    
    UIView * lastView = nil;
    NSArray * titles = @[@"请输入User ID",@"请输入API Key",@"请输入视频标题",@"请输入标签",@"请输入视频简介"];
    for (int i = 0; i < titles.count; i++) {
        UILabel * titleLabel = [[UILabel alloc]init];
        titleLabel.text = [titles objectAtIndex:i];
        titleLabel.font = TitleFont(14);
        titleLabel.textColor = TitleColor_51;
        [self.bgScrollView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@10);
            make.right.equalTo(@(-10));
//            make.top.equalTo(@(20 + (14 + 10 + 39 + 25) * i));
            make.top.equalTo(tsLabel.mas_bottom).offset(20 + (14 + 10 + 39 + 25) * i);
            make.height.equalTo(@14);
        }];
        
        UIView * bgView = [[UIView alloc]init];
        bgView.backgroundColor = [UIColor colorWithRed:243/255.0 green:244/255.0 blue:245/255.0 alpha:1.0];
        [self.bgScrollView addSubview:bgView];
        CGFloat bgViewHeight = 0;
        if (i == titles.count - 1) {
            bgViewHeight = 205;
            
            lastView = bgView;
        }else{
            bgViewHeight = 39;
        }

        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@10);
            make.width.equalTo(@(ScreenWidth - 20));
            make.top.equalTo(titleLabel.mas_bottom).offset(10);
            make.height.equalTo(@(bgViewHeight));
        }];
        
        if (i == titles.count - 1) {
            self.descriptionTextView = [[UITextView alloc]init];
            self.descriptionTextView.font = TitleFont(14);
            self.descriptionTextView.textColor = TitleColor_51;
            self.descriptionTextView.backgroundColor = bgView.backgroundColor;
            [bgView addSubview:self.descriptionTextView];
            [_descriptionTextView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(@10);
                make.right.equalTo(@(-10));
                make.top.equalTo(@10);
                make.bottom.equalTo(@(-10));
            }];
            
        }else{
            UITextField * textfield = [[UITextField alloc]init];
            textfield.placeholder = [titles objectAtIndex:i];
            textfield.textColor = TitleColor_51;
            textfield.font = TitleFont(14);
            textfield.delegate = self;
            textfield.tag = 100 + i;
            textfield.clearButtonMode = UITextFieldViewModeUnlessEditing;
            [bgView addSubview:textfield];
            [textfield mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(@10);
                make.right.equalTo(@(-10));
                make.top.and.bottom.equalTo(@0);
            }];
            
            if (i == 0) {
                self.userIdTextField = textfield;
            }
            if (i == 1) {
                self.apiKeyTextField = textfield;
            }
            if (i == 2) {
                self.titleTextField = textfield;
            }
            if (i == 3) {
                self.tagTextField = textfield;
            }
        }
    }
}

@end