// // StepViewController.m // watch // // Created by xTT on 2017/7/12. // Copyright © 2017年 xTT. All rights reserved. // #import "StepViewController.h" #import "Step.h" #import "PathView.h" #import "UILabel+BezierAnimation.h" #import "VRGCalendarView.h" @interface StepViewController () { VRGCalendarView *calendar; UIControl *_overlayView; } @property (strong, nonatomic) Step *infoStep; /** 环形圈 */ @property (strong, nonatomic) PathView *pathView; /** // 实际的走的步数 */ @property (strong, nonatomic) UILabel *creditLabel; /** 编辑目标步数的按钮 */ @property (strong, nonatomic) UIButton *editingGoalStepBtn; /** 编辑目标步数的输入框 */ @property (strong, nonatomic) UITextField *editingGoalTextField; /** 编辑目标步数的输入框 的View */ @property (strong, nonatomic) UIView *editingGoalView; @end @implementation StepViewController @synthesize myDataSource = _myDataSource; - (NSMutableArray *)myDataSource{ if (!_myDataSource) { _myDataSource = [[NSMutableArray alloc] initWithArray:@[@[@"今日步数",@"今日里程",@"消耗卡路里"]]]; } return _myDataSource; } - (Step *)infoStep{ if (!_infoStep) { _infoStep = [Step mj_objectWithKeyValues:[cUser.cDevice mj_JSONObject]]; // _infoStep.imei = cUser.cDevice.imei; // _infoStep.name = cUser.cDevice.name; // _infoStep.phone = cUser.cDevice.phone; // _infoStep.image = cUser.cDevice.image; // _infoStep.avator = cUser.cDevice.avator; // _infoStep.sex = cUser.cDevice.sex; } return _infoStep; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; } -(void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; [self dismissCalendarView]; } /** 重新加载动画 */ -(void)reLoadAnimation{ if(self.creditLabel){ [self.creditLabel animationFromnum:0 toNum:self.infoStep.num.floatValue duration:1 precision:@"%d"]; } self.heatLabel.text = [NSString stringWithFormat:@"%.03f",self.infoStep.calorie.floatValue]; self.distanceLabel.text = [NSString stringWithFormat:@"%.03f",self.infoStep.distance.floatValue]; if(self.pathView){ double turns = self.infoStep.num.doubleValue /self.infoStep.target.doubleValue; // double turns = 6123.0 /self.infoStep.target.doubleValue; [self.pathView StartAnimationWithStartColor:[UIColor redColor] EndColor:mainColor NumberOfTurns:turns AnimationDuration:2 Delay:0]; } } -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; } - (void)viewDidLoad { [super viewDidLoad]; [self setDayTimeLabelText:@"今天"]; [self.dayTimeBtn setTitleColor:mainColor forState:0]; [self initView]; [self getCurrentStepDateWithParam:nil]; } /** //MARK:----点击日期标题,显示日历的方法 */ - (IBAction)dayTimeBtnAction{ [self dismissCalendarView]; _overlayView = [[UIControl alloc]initWithFrame:self.view.bounds]; [_overlayView addTarget:self action:@selector(dismissCalendarView) forControlEvents:UIControlEventTouchUpInside]; calendar = [[VRGCalendarView alloc] init]; calendar.limitDay = 60; CGRect c = calendar.frame; c.origin.y = self.dayTimeBtn.frame.origin.y+self.dayTimeBtn.frame.size.height+10; calendar.frame = c; calendar.delegate=self; // NSDate *selectDate; //// if([self.dayTimeBtn.titleLabel.text isEqualToString:@"今天"]){ //// //今天 //// selectDate = [NSDate dateWithTimeIntervalSince1970:[NSDate date].timeIntervalSince1970]; //// }else if([self.dayTimeBtn.titleLabel.text isEqualToString:@"昨天"]){ //// //昨天 //// selectDate = [NSDate dateWithTimeIntervalSince1970:[NSDate date].timeIntervalSince1970 - 60*60*24]; //// }else{ //// NSDateFormatter * dateF = [[NSDateFormatter alloc]init]; //// [dateF setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]]; //// [dateF setDateFormat:@"yyyy-MM-dd"]; //// NSDate *date =[dateF dateFromString:self.dayTimeBtn.titleLabel.text]; //// selectDate = date; //// } // //今天 // selectDate = [NSDate dateWithTimeIntervalSince1970:[NSDate date].timeIntervalSince1970]; // calendar.selectedDate = selectDate; calendar.alpha = 1; [self.view addSubview:_overlayView]; [self.view addSubview:calendar]; } - (void)dismissCalendarView{ if(_overlayView){ [_overlayView removeFromSuperview]; _overlayView = nil; [UIView animateWithDuration:0.35 animations:^{ calendar.alpha = 0; }completion:^(BOOL finished) { [calendar removeFromSuperview]; }]; [self.dayTimeIconBtn setImage:[UIImage imageNamed:@"ic_arrow_drop_dowm_main"] forState:0]; } } #pragma mark - 日期选择Delegate -(void)calendarView:(VRGCalendarView *)calendarView switchedToMonth:(int)month targetHeight:(float)targetHeight animated:(BOOL)animated { } -(void)calendarView:(VRGCalendarView *)calendarView dateSelected:(NSDate *)date { NSString *selectDay = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" date:date]; //对比昨天和今天 //昨天 NSString *yesterday = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970 - 60*60*24]; //今天 NSString *today = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970]; if([selectDay isEqualToString:yesterday]){ [self setDayTimeLabelText:@"昨天"]; }else if([selectDay isEqualToString:today]){ [self setDayTimeLabelText:@"今天"]; }else{ [self setDayTimeLabelText:selectDay]; } [self getCurrentStepDateWithParam:nil]; [self dismissCalendarView]; } -(void)initView{ self.beforeDayBtn.layer.cornerRadius = 3; self.beforeDayBtn.layer.masksToBounds = YES; [self.beforeDayBtn setBackgroundColor:mainColor]; self.afterDayBtn.layer.cornerRadius = 3; self.afterDayBtn.layer.masksToBounds = YES; [self.afterDayBtn setBackgroundColor:mainColor]; self.distanceLabel.textColor = mainColor; self.heatLabel.textColor = mainColor; self.pathView = [[PathView alloc] initWithFrame:CGRectMake(SWIDTH*(1-(190.0/375.0))/2, (ScreenHeight-getRectNavAndStatusHight)/5.0, SWIDTH*190.0/375.0, SWIDTH*190.0/375.0) LineWidth:10]; [self.view addSubview:self.pathView]; // 实际的走的步数 self.creditLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SWIDTH*150/375.0, 21)]; self.creditLabel.center = self.pathView.center; self.creditLabel.font = [UIFont systemFontOfSize:28]; self.creditLabel.textColor =mainColor; self.creditLabel.textAlignment = 1; self.creditLabel.text = @"0"; [self.view addSubview:self.creditLabel]; UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake((SWIDTH-CGRectGetWidth(self.creditLabel.frame))/2, self.creditLabel.frame.origin.y- 21 -11,CGRectGetWidth(self.creditLabel.frame) , 21)]; tempLabel.text = @"步数"; tempLabel.textAlignment = 1; tempLabel.font = [UIFont systemFontOfSize:13]; [self.view addSubview:tempLabel]; self.editingGoalStepBtn = [UIButton buttonWithType:UIButtonTypeSystem]; [self.editingGoalStepBtn setImage:[UIImage imageNamed:@"icon_editor_orange"] forState:0]; self.editingGoalStepBtn.frame = CGRectMake((SWIDTH-CGRectGetWidth(self.creditLabel.frame))/2, self.creditLabel.frame.origin.y + 21 +11, CGRectGetWidth(self.creditLabel.frame), 21); [self.editingGoalStepBtn setTitle:[NSString stringWithFormat:@"目标:%d",self.infoStep.target.intValue] forState:0]; [self.editingGoalStepBtn setTintColor:mainColor]; [self.editingGoalStepBtn setTitleColor:[UIColor blackColor] forState:0]; [self.editingGoalStepBtn addTarget:self action:@selector(changeGoalStepNumAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.editingGoalStepBtn]; } - (IBAction)dayBtnAction:(UIButton *)sender { NSMutableDictionary * params = [NSMutableDictionary dictionary]; if([sender isEqual:self.beforeDayBtn]){ //按了前一天的按钮 NSString *paramsDay; if([self.dayTimeBtn.titleLabel.text isEqualToString:@"今天"]){ //昨天 paramsDay = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970 - 60*60*24]; [self setDayTimeLabelText: @"昨天"]; }else if([self.dayTimeBtn.titleLabel.text isEqualToString:@"昨天"]){ paramsDay = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970 - 2 * 60*60*24]; [self setDayTimeLabelText: paramsDay]; }else{ //当前位置的天数转换成的时间戳 NSTimeInterval tempTime = [myHelper getDateWithStr:self.dayTimeBtn.titleLabel.text dateFormat:@"yyyy-MM-dd"].timeIntervalSince1970; paramsDay = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:tempTime - 60*60*24]; [self setDayTimeLabelText: paramsDay]; } [params setValue:paramsDay forKey:@"date"]; }else if([sender isEqual:self.afterDayBtn]){ //后一天 if([self.dayTimeBtn.titleLabel.text isEqualToString:@"昨天"]){ [self setDayTimeLabelText:@"今天"]; params = nil; }else{ //当前位置的天数转换成的时间戳 NSTimeInterval tempTime = [myHelper getDateWithStr:self.dayTimeBtn.titleLabel.text dateFormat:@"yyyy-MM-dd"].timeIntervalSince1970; NSString *paramsDay = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:tempTime + 60*60*24]; //对比昨天和今天 //昨天 NSString *yesterday = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970 - 60*60*24]; //今天 NSString *today = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970]; if([paramsDay isEqualToString:yesterday]){ [self setDayTimeLabelText:@"昨天"]; }else if([paramsDay isEqualToString:today]){ [self setDayTimeLabelText:@"今天"]; }else{ [self setDayTimeLabelText:paramsDay]; } [params setValue:paramsDay forKey:@"date"]; } }else{ params = nil; } [self getCurrentStepDateWithParam:params]; } -(void)setDayTimeLabelText:(NSString*)day{ [self.dayTimeBtn setTitle:day forState:0]; if ([day isEqualToString:@"今天"]) { //可以设置 目标步数 [self.editingGoalStepBtn setImage:[UIImage imageNamed:@"icon_editor_orange"] forState:0]; [self setBtnBadRoGoodFlag:YES WithBtn:self.editingGoalStepBtn]; [self setBtnBadRoGoodFlag:NO WithBtn:self.afterDayBtn]; }else{ //不能设置 目标步数 [self.editingGoalStepBtn setImage:nil forState:0]; [self setBtnBadRoGoodFlag:NO WithBtn:self.editingGoalStepBtn]; [self setBtnBadRoGoodFlag:YES WithBtn:self.afterDayBtn]; } } /** 查询 当前日期 的计步数据 */ -(void)getCurrentStepDateWithParam:(NSMutableDictionary*)params{ // 查询我的排行榜 if (!params) { params = [NSMutableDictionary dictionary]; if([self.dayTimeBtn.titleLabel.text isEqualToString:@"今天"]){ //今天 NSString *today = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970]; [params setValue:today forKey:@"date"]; }else if([self.dayTimeBtn.titleLabel.text isEqualToString:@"昨天"]){ //昨天 NSString *yesterday = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970 - 60*60*24]; [params setValue:yesterday forKey:@"date"]; }else{ [params setValue:self.dayTimeBtn.titleLabel.text forKey:@"date"]; } } WEAKSELF [self.infoStep getObjsParams: params Success:^(Step *data) { weakSelf.infoStep = data; [weakSelf.editingGoalStepBtn setTitle:[NSString stringWithFormat:@"目标:%@",weakSelf.infoStep.target] forState:0]; [weakSelf reLoadAnimation]; } failure:^(NSError *error) { }]; } /** 设置按钮不可选且半透明 @param flag Bad is NO , Good is YES @param button button */ -(void)setBtnBadRoGoodFlag:(BOOL)flag WithBtn:(UIButton*) button{ if(flag){ [button setAlpha:1]; [button setEnabled:YES]; }else{ [button setAlpha:0.5]; [button setEnabled:NO]; } } -(void)changeGoalStepNumAction:(UIButton*)sender{ if(!self.editingGoalView){ self.editingGoalView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)]; self.editingGoalView.backgroundColor = [UIColor whiteColor]; CGFloat alertViewWidth = (320.f - 20*2 - 20*2); self.editingGoalTextField = [[UITextField alloc] initWithFrame:CGRectMake((ScreenWidth - alertViewWidth)/2, 1, alertViewWidth, 48)]; self.editingGoalTextField.borderStyle = UITextBorderStyleRoundedRect; self.editingGoalTextField.layer.borderColor = RGB(229, 229, 229).CGColor; self.editingGoalTextField.backgroundColor = RGB(250, 250, 250); self.editingGoalTextField.placeholder = @"请输入目标步数"; self.editingGoalTextField.keyboardType = UIKeyboardTypeNumberPad; [self.editingGoalView addSubview:self.editingGoalTextField]; } self.editingGoalTextField.text = self.infoStep.target.stringValue; WEAKSELF LGAlertView *alertView = [[LGAlertView alloc] initWithViewAndTitle:@"目标步数" message:@"" style:LGAlertViewStyleAlert view:self.editingGoalView buttonTitles:@[@"确定"] cancelButtonTitle:@"取消" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { [weakSelf.editingGoalTextField resignFirstResponder]; weakSelf.infoStep.target = [NSNumber numberWithInt:weakSelf.editingGoalTextField.text.intValue]; [weakSelf.editingGoalStepBtn setTitle:[NSString stringWithFormat:@"目标步数:%@",weakSelf.infoStep.target] forState:0]; xLog(@"修改 目标步数 %@",weakSelf.infoStep.target); NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setValue:weakSelf.infoStep.target forKey:@"target"]; [weakSelf.infoStep postGoalStepParams:dic Success:^(Step *data) { [weakSelf getCurrentStepDateWithParam:nil]; } failure:^(NSError *error) { [weakSelf getCurrentStepDateWithParam:nil]; }]; } cancelHandler:^(LGAlertView *alertView) { [weakSelf.editingGoalTextField resignFirstResponder]; } destructiveHandler:nil]; [alertView showAnimated:YES completionHandler:nil]; // LGAlertView *alertView = [[LGAlertView alloc] initWithTextFieldsAndTitle:@"目标步数" message:@"" numberOfTextFields:1 textFieldsSetupHandler:^(UITextField *textField, NSUInteger index) { // // textField.delegate = weakSelf; // textField.keyboardType = UIKeyboardTypeNumberPad; // // } buttonTitles:@[@"确定"] cancelButtonTitle:@"取消" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { // // // weakSelf.infoStep.target = [NSNumber numberWithInt:[[alertView.textFieldsArray.firstObject text] intValue]]; // [weakSelf.editingGoalStepBtn setTitle:[NSString stringWithFormat:@"目标步数:%@",weakSelf.infoStep.target] forState:0]; // // xLog(@"修改 目标步数 %@",weakSelf.infoStep.target); // NSMutableDictionary *dic = [NSMutableDictionary dictionary]; // [dic setValue:weakSelf.infoStep.target forKey:@"target"]; // [weakSelf.infoStep postGoalStepParams:dic Success:^(Step *data) { // [weakSelf reLoadAnimation]; // } failure:^(NSError *error) { // [weakSelf reLoadAnimation]; // }]; // // } cancelHandler:nil destructiveHandler:nil]; // // [alertView showAnimated:YES completionHandler:nil]; } //MARK: UITextFieldDelegate 限制字数 和输入的 字符 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ if([@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" containsString:string]){ // 最终的结果 NSString *str = textField.text; NSString *resultStr = [str stringByReplacingCharactersInRange:range withString:string]; if(resultStr.length > 5){ return NO; }else{ return YES; } }else{ if([string isEqualToString:@""]){ return YES; } return NO; } } @end