|
@@ -0,0 +1,384 @@
|
|
1
|
+//
|
|
2
|
+// KXInstallmentViewController.m
|
|
3
|
+// CAISHEN
|
|
4
|
+//
|
|
5
|
+// Created by jcymac on 2017/11/9.
|
|
6
|
+// Copyright © 2017年 kuxuan. All rights reserved.
|
|
7
|
+//
|
|
8
|
+
|
|
9
|
+#import "KXInstallmentViewController.h"
|
|
10
|
+#import <ReactiveCocoa/ReactiveCocoa.h>
|
|
11
|
+
|
|
12
|
+@interface KXInstallmentViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
|
|
13
|
+@property(nonatomic,strong)UITableView *tableView;
|
|
14
|
+
|
|
15
|
+@property(nonatomic,strong)UIView *headView;
|
|
16
|
+@property(nonatomic,strong)UIView *footerView;
|
|
17
|
+@property(nonatomic,copy)NSString *result;
|
|
18
|
+@property(nonatomic,strong)NSArray *array;
|
|
19
|
+@property(nonatomic,strong)UITextField *money;
|
|
20
|
+@property(nonatomic,strong)UITextField *date;
|
|
21
|
+@property(nonatomic,strong)UITextField *rate;
|
|
22
|
+@property(nonatomic,strong)UIButton *btn;
|
|
23
|
+@property(nonatomic,strong)UILabel *lableDown;
|
|
24
|
+@property(nonatomic,strong)UIView *leftView;
|
|
25
|
+@property(nonatomic,strong)UIView *RightView;
|
|
26
|
+@property(nonatomic,strong)UILabel *paymentlLabel;
|
|
27
|
+@property(nonatomic,strong)UILabel *repaymentLabel;
|
|
28
|
+
|
|
29
|
+@property(nonatomic,assign)float resultInt;
|
|
30
|
+@end
|
|
31
|
+
|
|
32
|
+@implementation KXInstallmentViewController
|
|
33
|
+
|
|
34
|
+- (void)viewDidLoad {
|
|
35
|
+ [super viewDidLoad];
|
|
36
|
+ // Do any additional setup after loading the view.
|
|
37
|
+ [self createNavBar];
|
|
38
|
+ [self setupView];
|
|
39
|
+ [self setPosition];
|
|
40
|
+}
|
|
41
|
+-(void)setPosition{
|
|
42
|
+ //左边数字
|
|
43
|
+ CGRect leftRect=self.leftView.bounds;
|
|
44
|
+ leftRect.size.height=leftRect.size.height/2;
|
|
45
|
+ leftRect.origin.y=leftRect.size.height;
|
|
46
|
+ self.paymentlLabel.frame=leftRect;
|
|
47
|
+ //右边数字
|
|
48
|
+ CGRect rightRect=self.RightView.bounds;
|
|
49
|
+ rightRect.size.height=rightRect.size.height/2;
|
|
50
|
+ rightRect.origin.y=rightRect.size.height;
|
|
51
|
+ self.repaymentLabel.frame=rightRect;
|
|
52
|
+}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+-(UILabel *)paymentlLabel{//左边那个
|
|
56
|
+ if (!_paymentlLabel) {
|
|
57
|
+ _paymentlLabel=[[UILabel alloc]init];
|
|
58
|
+ _paymentlLabel.textColor=[UIColor CHColorWithHex:0x000000];
|
|
59
|
+ _paymentlLabel.font=[UIFont systemFontOfSize:26/2];
|
|
60
|
+ _paymentlLabel.textAlignment=NSTextAlignmentCenter;
|
|
61
|
+ _paymentlLabel.text=@"0";
|
|
62
|
+ }
|
|
63
|
+ return _paymentlLabel;
|
|
64
|
+
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+-(UILabel *)repaymentLabel{//右边那个
|
|
68
|
+ if (!_repaymentLabel) {
|
|
69
|
+ _repaymentLabel=[[UILabel alloc]init];
|
|
70
|
+ _repaymentLabel.textColor=[UIColor CHColorWithHex:0x000000];
|
|
71
|
+ _repaymentLabel.font=[UIFont systemFontOfSize:26/2];
|
|
72
|
+ _repaymentLabel.textAlignment=NSTextAlignmentCenter;
|
|
73
|
+ _repaymentLabel.text=@"0";
|
|
74
|
+ }
|
|
75
|
+ return _repaymentLabel;
|
|
76
|
+}
|
|
77
|
+
|
|
78
|
+-(UIView *)leftView{
|
|
79
|
+ if (!_leftView) {
|
|
80
|
+ _leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH/2, 80)];
|
|
81
|
+// _leftView.backgroundColor=[UIColor redColor];
|
|
82
|
+ //支付利息(元)
|
|
83
|
+ CGRect rect=_leftView.bounds;
|
|
84
|
+ rect.size.height=rect.size.height/2;
|
|
85
|
+ UILabel *label=[[UILabel alloc]initWithFrame:rect];
|
|
86
|
+ label.text=@"支付利息(元)";
|
|
87
|
+ label.textAlignment=NSTextAlignmentCenter;
|
|
88
|
+ label.font=[UIFont systemFontOfSize:22/2];
|
|
89
|
+ label.textColor=[UIColor CHColorWithHex:0x414141];
|
|
90
|
+ [_leftView addSubview:label];
|
|
91
|
+ }
|
|
92
|
+ return _leftView;
|
|
93
|
+}
|
|
94
|
+
|
|
95
|
+-(UIView *)RightView{
|
|
96
|
+ if (!_RightView) {
|
|
97
|
+ _RightView=[[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2, 100, SCREEN_WIDTH/2, 80)];
|
|
98
|
+ CGRect rect=_RightView.bounds;
|
|
99
|
+ rect.size.height=rect.size.height/2;
|
|
100
|
+ UILabel *label=[[UILabel alloc]initWithFrame:rect];
|
|
101
|
+ label.text=@"还款总额(元)";
|
|
102
|
+ label.textAlignment=NSTextAlignmentCenter;
|
|
103
|
+ label.font=[UIFont systemFontOfSize:22/2];
|
|
104
|
+ label.textColor=[UIColor CHColorWithHex:0x414141];
|
|
105
|
+ [_RightView addSubview:label];
|
|
106
|
+ }
|
|
107
|
+ return _RightView;
|
|
108
|
+}
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+-(UIButton *)btn{
|
|
112
|
+ if (!_btn) {
|
|
113
|
+ _btn= [UIButton buttonWithType:UIButtonTypeCustom];
|
|
114
|
+ _btn.frame=CGRectMake(0, 0, 484/2, 80/2);
|
|
115
|
+ _btn.backgroundColor=[UIColor baseColor];
|
|
116
|
+ [_btn setTitle:@"我要分期" forState:UIControlStateNormal];
|
|
117
|
+ _btn.titleLabel.font=[UIFont systemFontOfSize:FITSIZE(14)];
|
|
118
|
+ [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
|
119
|
+ _btn.layer.cornerRadius = 80/4;
|
|
120
|
+ [_btn addTarget:self action:@selector(individualIncomeGo:) forControlEvents:UIControlEventTouchUpInside];
|
|
121
|
+ }
|
|
122
|
+ return _btn;
|
|
123
|
+}
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+-(void)individualIncomeGo:(UIButton *)sender{
|
|
128
|
+ //总利息
|
|
129
|
+ self.paymentlLabel.text=[self individualIncomeSalary:self.money.text Threshold:self.date.text];
|
|
130
|
+ //还款总额
|
|
131
|
+ self.repaymentLabel.text=[NSString stringWithFormat:@"%.2f",([self.paymentlLabel.text floatValue]+[self.money.text intValue])];
|
|
132
|
+ //每月月供
|
|
133
|
+ self.lableDown.text=[NSString stringWithFormat:@"%.2f",([self.repaymentLabel.text floatValue]/[self.date.text intValue])];
|
|
134
|
+}
|
|
135
|
+-(NSString * )individualIncomeSalary:(NSString *)money Threshold:(NSString *)date{
|
|
136
|
+ NSInteger moneyInt=[money intValue];
|
|
137
|
+ NSInteger dateInt=[date intValue];
|
|
138
|
+ NSString *result=[NSString stringWithFormat:@"%.2f",moneyInt*dateInt*0.0048];
|
|
139
|
+ return result;
|
|
140
|
+}
|
|
141
|
+-(NSArray *)array{
|
|
142
|
+ if (!_array) {
|
|
143
|
+ _array= [NSArray arrayWithObjects:
|
|
144
|
+ @{@"name":@"分期金额",@"placeholder":@"请输入金额"},
|
|
145
|
+ @{@"name":@"分期期数",@"placeholder":@"请输入期数"},
|
|
146
|
+ @{@"name":@"月利率(%)",@"text":@"0.48"},nil];
|
|
147
|
+ }
|
|
148
|
+ return _array;
|
|
149
|
+}
|
|
150
|
+
|
|
151
|
+-(NSString *)result{
|
|
152
|
+ return [NSString stringWithFormat:@"%.2f",self.resultInt];
|
|
153
|
+
|
|
154
|
+}
|
|
155
|
+- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
|
|
156
|
+ return 30;
|
|
157
|
+
|
|
158
|
+}
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
|
|
162
|
+ UIView *header=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
|
|
163
|
+ UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(34/2, 0, SCREEN_WIDTH-34, 30)];
|
|
164
|
+ label.text=@"请输入分期信息";
|
|
165
|
+ label.font=[UIFont systemFontOfSize:26/2];
|
|
166
|
+ label.textColor=[UIColor CHColorWithHex:0x9a9a9a];
|
|
167
|
+ [header addSubview:label];
|
|
168
|
+ return header;
|
|
169
|
+}
|
|
170
|
+
|
|
171
|
+-(UIView *)footerView{
|
|
172
|
+ if (!_footerView) {
|
|
173
|
+ _footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
|
|
174
|
+ [_footerView addSubview:self.btn];
|
|
175
|
+ self.btn.center=_footerView.center;
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ return _footerView;
|
|
179
|
+}
|
|
180
|
+
|
|
181
|
+-(UILabel *)lableDown{
|
|
182
|
+ if (!_lableDown) {
|
|
183
|
+ _lableDown=[[UILabel alloc]init];
|
|
184
|
+ _lableDown.text=@"0";
|
|
185
|
+ _lableDown.font=[UIFont systemFontOfSize:40/2];
|
|
186
|
+ _lableDown.textColor=[UIColor CHColorWithHex:0xe42a4a];
|
|
187
|
+ _lableDown.textAlignment=NSTextAlignmentCenter;
|
|
188
|
+ }
|
|
189
|
+ return _lableDown;
|
|
190
|
+}
|
|
191
|
+-(UIView *)headView{
|
|
192
|
+ if (!_headView) {
|
|
193
|
+ _headView=[[UIView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, 200)];
|
|
194
|
+ _headView.backgroundColor=[UIColor whiteColor];
|
|
195
|
+ UILabel *lableTop=[[UILabel alloc]initWithFrame:CGRectMake(0, 50/2, SCREEN_WIDTH, 30)];
|
|
196
|
+ lableTop.text=@"每月月供(元)";
|
|
197
|
+ lableTop.font=[UIFont systemFontOfSize:26/2];
|
|
198
|
+ lableTop.textColor=[UIColor CHColorWithHex:0x414141];
|
|
199
|
+ lableTop.textAlignment=NSTextAlignmentCenter;
|
|
200
|
+ self.lableDown.frame=CGRectMake(0, lableTop.frame.origin.y+lableTop.frame.size.height+10, SCREEN_WIDTH, 30);
|
|
201
|
+ [_headView addSubview:lableTop];
|
|
202
|
+ [_headView addSubview:self.lableDown];
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+ }
|
|
206
|
+ return _headView;
|
|
207
|
+}
|
|
208
|
+-(UITableView *)tableView{
|
|
209
|
+ if (!_tableView) {
|
|
210
|
+ _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, NavBarHeight,SCREEN_WIDTH, SCREEN_HEIGHT - NavBarHeight) style:UITableViewStylePlain];
|
|
211
|
+ _tableView.tableHeaderView=self.headView;
|
|
212
|
+ _tableView.tableFooterView=self.footerView;
|
|
213
|
+ _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
|
|
214
|
+ _tableView.delegate=self;
|
|
215
|
+ _tableView.dataSource=self;
|
|
216
|
+ [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"individualIncome"];
|
|
217
|
+ }
|
|
218
|
+ return _tableView;
|
|
219
|
+}
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+-(void)setupView{
|
|
225
|
+ [self.view addSubview:self.tableView];
|
|
226
|
+ [self.headView addSubview:self.leftView];
|
|
227
|
+ [self.headView addSubview:self.RightView];
|
|
228
|
+ [self.leftView addSubview:self.paymentlLabel];
|
|
229
|
+ [self.RightView addSubview:self.repaymentLabel];
|
|
230
|
+
|
|
231
|
+}
|
|
232
|
+-(void)createNavBar{
|
|
233
|
+ self.navigationBar.backgroundColor = [UIColor checkBaseColor];
|
|
234
|
+ [self.navigationBar setNavTitle:@"分期计算器"];
|
|
235
|
+ self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
|
|
236
|
+ self.navigationBar.navTitleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size: 18];
|
|
237
|
+ UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
|
|
238
|
+ [leftBtn setImage:[UIImage imageNamed:@"back_black"] forState:UIControlStateNormal];
|
|
239
|
+ [[leftBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
|
|
240
|
+ [self.navigationController popViewControllerAnimated:YES];
|
|
241
|
+ }];
|
|
242
|
+ [self.navigationBar setCustomLeftButtons:@[leftBtn]];
|
|
243
|
+}
|
|
244
|
+
|
|
245
|
+//RAC 添加信号,处理计算按钮是否可点击
|
|
246
|
+- (void)addSingal {
|
|
247
|
+ UITextField *textField1 = (UITextField *)[self.view viewWithTag:1000+0];
|
|
248
|
+ UITextField *textField2 = (UITextField *)[self.view viewWithTag:1000+1];
|
|
249
|
+ UITextField *textField3 = (UITextField *)[self.view viewWithTag:1000+2];
|
|
250
|
+
|
|
251
|
+ if (!(textField1 && textField2)) {
|
|
252
|
+ return;
|
|
253
|
+ }
|
|
254
|
+
|
|
255
|
+ RACSignal *textSingal1 = [textField1.rac_textSignal map:^id(NSString * value) {
|
|
256
|
+ return @(value.length > 0);
|
|
257
|
+ }];
|
|
258
|
+ RACSignal *textSingal2 = [textField2.rac_textSignal map:^id(NSString * value) {
|
|
259
|
+ return @(value.length > 0);
|
|
260
|
+ }];
|
|
261
|
+ RACSignal *textSingal3 = [textField3.rac_textSignal map:^id(NSString * value) {
|
|
262
|
+ return @(value.length > 0);
|
|
263
|
+ }];
|
|
264
|
+
|
|
265
|
+ RACSignal *calculActiveSignal = [RACSignal
|
|
266
|
+ combineLatest:@[
|
|
267
|
+ textSingal1,
|
|
268
|
+ textSingal2,
|
|
269
|
+ textSingal3
|
|
270
|
+ ]
|
|
271
|
+ reduce:^id(NSNumber *textSingal1, NSNumber *textSingal2, NSNumber *textSingal3) {
|
|
272
|
+ return @([textSingal1 boolValue] && [textSingal2 boolValue] && [textSingal3 boolValue]);
|
|
273
|
+ }];
|
|
274
|
+
|
|
275
|
+ [calculActiveSignal subscribeNext:^(NSNumber *calculEnble) {
|
|
276
|
+ self.btn.enabled = [calculEnble boolValue];
|
|
277
|
+ self.btn.backgroundColor = [UIColor checkBaseColor];
|
|
278
|
+ [self.btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
|
279
|
+ }];
|
|
280
|
+}
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
|
|
284
|
+ return 98/2;
|
|
285
|
+}
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+#pragma mark UITableViewDataSource
|
|
289
|
+
|
|
290
|
+-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
|
|
291
|
+{
|
|
292
|
+ if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
|
|
293
|
+ [self addSingal];
|
|
294
|
+ }
|
|
295
|
+}
|
|
296
|
+
|
|
297
|
+- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
|
|
298
|
+ return self.array.count;
|
|
299
|
+}
|
|
300
|
+
|
|
301
|
+- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
|
|
302
|
+ UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"individualIncome"];
|
|
303
|
+ if (!cell) {
|
|
304
|
+
|
|
305
|
+ cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"individualIncome"];
|
|
306
|
+ }
|
|
307
|
+ NSDictionary *dic=self.array[indexPath.row];
|
|
308
|
+ cell.selectionStyle = UITableViewCellSelectionStyleNone;
|
|
309
|
+ UITextField *text=[[UITextField alloc]initWithFrame:CGRectMake(34/2,0, SCREEN_WIDTH-34,cell.bounds.size.height-1)];
|
|
310
|
+ text.tag = 1000 + indexPath.row;
|
|
311
|
+ text.delegate=self;
|
|
312
|
+ text.placeholder=dic[@"placeholder"];
|
|
313
|
+ text.text=dic[@"text"];
|
|
314
|
+ [text setFont:[UIFont systemFontOfSize:26/2]];
|
|
315
|
+ text.textColor=[UIColor CHColorWithHex:0x414141];
|
|
316
|
+ text.keyboardType=UIKeyboardTypeNumberPad;
|
|
317
|
+ text.textAlignment=NSTextAlignmentRight;
|
|
318
|
+ UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,100, text.bounds.size.height)];
|
|
319
|
+ label.text=dic[@"name"];
|
|
320
|
+ [label setFont:[UIFont systemFontOfSize:26/2]];
|
|
321
|
+ label.textColor=[UIColor CHColorWithHex:0x414141];
|
|
322
|
+ text.leftView=label;
|
|
323
|
+ text.leftViewMode=UITextFieldViewModeAlways;
|
|
324
|
+ [cell addSubview:text];
|
|
325
|
+ //自定义下划线
|
|
326
|
+ if (indexPath.row<2) {
|
|
327
|
+ UIView *separator=[[UIView alloc]initWithFrame:CGRectMake(34/2, cell.bounds.size.height-0.5, SCREEN_WIDTH-34, 0.5)];
|
|
328
|
+ separator.backgroundColor=[UIColor CHColorWithHex:0xe6e8e6];
|
|
329
|
+ [cell addSubview:separator];
|
|
330
|
+ }
|
|
331
|
+// else{
|
|
332
|
+// cell.userInteractionEnabled=NO;
|
|
333
|
+// }
|
|
334
|
+ //计算数据
|
|
335
|
+ switch (indexPath.row) {
|
|
336
|
+ case 0:
|
|
337
|
+ self.money=text;
|
|
338
|
+ break;
|
|
339
|
+ case 1:
|
|
340
|
+ self.date=text;
|
|
341
|
+ break;
|
|
342
|
+ case 2:
|
|
343
|
+ text.keyboardType=UIKeyboardTypeDecimalPad;
|
|
344
|
+ self.rate=text;
|
|
345
|
+ }
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+ return cell;
|
|
349
|
+}
|
|
350
|
+
|
|
351
|
+//只允许输入数字和点
|
|
352
|
+- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
|
|
353
|
+ if (range.length==1) {
|
|
354
|
+ return YES;
|
|
355
|
+ }
|
|
356
|
+ NSLog(@"aa");
|
|
357
|
+ //有点
|
|
358
|
+ if (textField==self.rate) {
|
|
359
|
+ if (([textField.text rangeOfString:@"."].location!=NSNotFound )&& ([string isEqualToString:@"."])) {
|
|
360
|
+ return NO;
|
|
361
|
+ }
|
|
362
|
+ if ([@"0123456789." rangeOfString:string].location == NSNotFound) {
|
|
363
|
+ return NO;
|
|
364
|
+ }
|
|
365
|
+ if([textField.text length]>10){
|
|
366
|
+ return NO;
|
|
367
|
+ }
|
|
368
|
+ }else{
|
|
369
|
+
|
|
370
|
+ if ([@"0123456789" rangeOfString:string].location == NSNotFound) {
|
|
371
|
+ return NO;
|
|
372
|
+ }
|
|
373
|
+ if([textField.text length]>10){
|
|
374
|
+ return NO;
|
|
375
|
+ }
|
|
376
|
+
|
|
377
|
+ }
|
|
378
|
+ return YES;
|
|
379
|
+}
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+@end
|