| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- //
- // EditorSkillsController.m
- // model
- //
- // Created by zuxiukuan on 2018/7/17.
- // Copyright © 2018年 Mine. All rights reserved.
- // 编辑技能
- #import "EditorSkillsController.h"
- @interface EditorSkillsController () <UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
- @property(nonatomic, strong) UIScrollView *bgScrollView;
- @property(nonatomic, strong) UITextField *workTypeTF; //工作类型
- @property(nonatomic, strong) UIImageView *workPicView; //工作照片
- @property(nonatomic, strong) UITextField *priceTF; //价格
- //
- @property(nonatomic, copy) NSString *imageStr; //图片url
- @end
- @implementation EditorSkillsController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
- UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
- tapGestureRecognizer.cancelsTouchesInView = NO;
- [self.view addGestureRecognizer:tapGestureRecognizer];
- [self creatNavBar];
- [self creatSubViews];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- [self.navigationController setNavigationBarHidden:NO animated:YES];
- }
- #pragma mark -- 收起键盘
- - (void)keyboardHide:(UITapGestureRecognizer *)tap {
- [self.view endEditing:YES];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)creatNavBar {
- self.title = @"编辑技能";
- //
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
- btn.frame = CGRectMake(0, 0, 40, 40);
- [btn setImage:[UIImage imageNamed:@"fanhui2"] forState:UIControlStateNormal];
- btn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
- [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
- UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
- UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
- // nagetiveSpacer.width = -12;//这个值可以根据自己需要自己调整
- self.navigationItem.leftBarButtonItems = @[nagetiveSpacer, leftItem];
- }
- - (void)backClick {
- [self.navigationController popViewControllerAnimated:YES];
- }
- - (void)creatSubViews {
- //
- UIScrollView *scrollView = [[UIScrollView alloc] init];
- scrollView.frame = self.view.bounds;
- scrollView.showsVerticalScrollIndicator = NO;
- scrollView.backgroundColor = [UIColor whiteColor];
- CGFloat height = ScreenHeight < 667 ? 697 : ScreenHeight;
- scrollView.contentSize = CGSizeMake(ScreenWidth, height);
- [self.view addSubview:scrollView];
- self.bgScrollView = scrollView;
- //
- UILabel *workTypeTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 80, 15)];
- workTypeTitleLabel.text = @"工作类型:";
- workTypeTitleLabel.font = [UIFont systemFontOfSize:15];
- workTypeTitleLabel.textColor = RGBValueColor(0x333333, 1.0);
- [scrollView addSubview:workTypeTitleLabel];
- UITextField *workTypeTF = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(workTypeTitleLabel.frame), 20, ScreenWidth / 2, 15)];
- // workTypeTF.placeholder = @"选择您的工作类型";
- workTypeTF.text = self.skilldic[@"name"];
- workTypeTF.userInteractionEnabled = NO;
- workTypeTF.font = [UIFont systemFontOfSize:15];
- workTypeTF.textColor = RGBValueColor(0x333333, 1.0);
- [scrollView addSubview:workTypeTF];
- self.workTypeTF = workTypeTF;
- //
- UIView *upLineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(workTypeTitleLabel.frame) + 20, ScreenWidth, 0.5)];
- upLineView.backgroundColor = RGBValueColor(0xdfdfdf, 1.0);
- [scrollView addSubview:upLineView];
- UILabel *workPicTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(upLineView.frame) + 20, 80, 15)];
- workPicTitleLabel.text = @"工作照片:";
- workPicTitleLabel.font = [UIFont systemFontOfSize:15];
- workPicTitleLabel.textColor = RGBValueColor(0x333333, 1.0);
- [scrollView addSubview:workPicTitleLabel];
- self.workPicView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(workPicTitleLabel.frame), CGRectGetMaxY(upLineView.frame) + 20, 80, 80)];
- NSString *skillphoto = self.skilldic[@"skillphoto"];
- NSString *urlStr;
- if ([skillphoto containsString:@"http"]) {
- urlStr = [NSString stringWithFormat:@"%@", skillphoto];
- } else {
- urlStr = [NSString stringWithFormat:@"%@/%@", PublicUrl, skillphoto];
- }
- [self.workPicView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"tianjiajineng"]];
- [scrollView addSubview:self.workPicView];
- UIButton *addPicBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(workPicTitleLabel.frame), CGRectGetMaxY(upLineView.frame) + 20, 80, 80)];
- addPicBtn.backgroundColor = [UIColor clearColor];
- [addPicBtn addTarget:self action:@selector(addPicBtnAction) forControlEvents:UIControlEventTouchUpInside];
- [scrollView addSubview:addPicBtn];
- UIView *downLineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.workPicView.frame) + 20, ScreenWidth, 0.5)];
- downLineView.backgroundColor = RGBValueColor(0xdfdfdf, 1.0);
- [scrollView addSubview:downLineView];
- //
- UILabel *priceTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(downLineView.frame) + 20, 50, 15)];
- priceTitleLabel.text = @"价格:";
- priceTitleLabel.font = [UIFont systemFontOfSize:15];
- priceTitleLabel.textColor = RGBValueColor(0x333333, 1.0);
- [scrollView addSubview:priceTitleLabel];
- UITextField *priceTF = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(priceTitleLabel.frame), CGRectGetMaxY(downLineView.frame) + 20, 100, 15)];
- priceTF.textAlignment = NSTextAlignmentLeft;
- priceTF.keyboardType = UIKeyboardTypeDecimalPad;
- priceTF.font = [UIFont systemFontOfSize:15];
- priceTF.textColor = RGBValueColor(0x333333, 1.0);
- priceTF.placeholder = @"请填写价格";
- priceTF.text = self.skilldic[@"price"];
- priceTF.tag = 700;
- priceTF.delegate = self;
- [scrollView addSubview:priceTF];
- self.priceTF = priceTF;
- UILabel *unitLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.priceTF.frame), CGRectGetMaxY(downLineView.frame) + 20, 100, 15)];
- unitLabel.textAlignment = NSTextAlignmentLeft;
- unitLabel.text = @"元/小时";
- unitLabel.font = [UIFont systemFontOfSize:15];
- unitLabel.textColor = RGBValueColor(0x333333, 1.0);
- [scrollView addSubview:unitLabel];
- //
- UIButton *postBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- postBtn.frame = CGRectMake(20, CGRectGetMaxY(priceTitleLabel.frame) + 70, ScreenWidth - 40, 45);
- [postBtn setBackgroundImage:[UIImage imageNamed:@"anniu"] forState:UIControlStateNormal];
- [postBtn setTitle:@"确认修改" forState:UIControlStateNormal];
- [postBtn setTitleColor:RGBValueColor(0xffffff, 1.0) forState:UIControlStateNormal];
- [postBtn addTarget:self action:@selector(postBtnAction) forControlEvents:UIControlEventTouchUpInside];
- [scrollView addSubview:postBtn];
- //
- UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- deleteBtn.frame = CGRectMake(20, CGRectGetMaxY(postBtn.frame) + 19, ScreenWidth - 40, 45);
- [deleteBtn setBackgroundImage:[UIImage imageNamed:@"chakan"] forState:UIControlStateNormal];
- [deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
- [deleteBtn setTitleColor:RGBValueColor(0xfe4086, 1.0) forState:UIControlStateNormal];
- [deleteBtn addTarget:self action:@selector(deleteBtnAction) forControlEvents:UIControlEventTouchUpInside];
- [scrollView addSubview:deleteBtn];
- //
- UILabel *instructionsLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(deleteBtn.frame) + 16, ScreenWidth - 40, 12)];
- instructionsLabel.text = @"说明:请勿发布违反国家法规的内容";
- instructionsLabel.font = [UIFont systemFontOfSize:12];
- instructionsLabel.textColor = RGBValueColor(0x333333, 1.0);
- [scrollView addSubview:instructionsLabel];
- }
- #pragma mark -- 确认修改
- - (void)postBtnAction {
- NSLog(@"确认修改");
- //修改没有图片
- [self updateSkillFromNetworking];
- }
- - (void)deleteBtnAction {
- NSLog(@"删除");
- [self getRemoveModelSkillFromNetworking];
- }
- #pragma mark -- 确认修改
- - (void)updateSkillFromNetworking {
- [MBProgressHUD showHUDAddedTo:self.view animated:YES];
- NSString *modelpk = self.skilldic[@"modelpk"];
- NSString *skillpk = self.skilldic[@"skillpk"];
- NSString *pk = self.skilldic[@"modelpricepk"];
- NSString *price = [NSString stringWithFormat:@"%@", self.priceTF.text];
- NSMutableDictionary *parameterDic = [NSMutableDictionary dictionaryWithCapacity:0];
- [parameterDic setObject:modelpk forKey:@"modelpk"];
- [parameterDic setObject:skillpk forKey:@"skillpk"];
- [parameterDic setObject:pk forKey:@"pk"];
- if (self.imageStr.length > 0) {
- [parameterDic setObject:self.imageStr forKey:@"skillphoto"]; //返回的url
- }
- [parameterDic setObject:price forKey:@"price"];
- [parameterDic setObject:@"元" forKey:@"unit"];
- NSLog(@"%@", parameterDic);
- [YanCNetWorkManager requestPostWithURLStr:Url_updateSkill(PublicUrl) parameters:parameterDic finish:^(id dataDic) {
- [MBProgressHUD hideHUDForView:self.view animated:YES];
- NSString *issuccess = dataDic[@"msg"];
- if ([issuccess isEqualToString:@"success"]) {
- [MBProgressHUD showInfo:@"修改成功!"];
- } else {
- [MBProgressHUD showInfo:@"请求失败!"];
- }
- } enError:^(NSError *error) {
- [MBProgressHUD hideHUDForView:self.view animated:YES];
- }];
- }
- #pragma mark -- 删除
- - (void)getRemoveModelSkillFromNetworking {
- [MBProgressHUD showHUDAddedTo:self.view animated:YES];
- // ModelUser *modelUser = [ModelUser user];
- NSString *modelpk = self.skilldic[@"modelpk"];
- NSString *skillpk = self.skilldic[@"modelpricepk"];
- NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:modelpk, @"modelpk", skillpk, @"skillpk", nil];
- [YanCNetWorkManager requestPostWithURLStr:Url_removemodelskill(PublicUrl) parameters:dic finish:^(id dataDic) {
- [MBProgressHUD hideHUDForView:self.view animated:YES];
- NSString *issuccess = dataDic[@"msg"];
- if ([issuccess isEqualToString:@"success"]) {
- [MBProgressHUD showInfo:@"删除成功!"];
- [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(backClick) userInfo:nil repeats:NO];
- } else {
- [MBProgressHUD showInfo:@"操作失败!"];
- }
- } enError:^(NSError *error) {
- [MBProgressHUD hideHUDForView:self.view animated:YES];
- }];
- }
- #pragma mark -- 添加工作照
- - (void)addPicBtnAction {
- NSLog(@"添加工作照");
- [self changeHeaderPicture];
- }
- - (void)changeHeaderPicture {
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
- // 设置按钮
- UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"从相册中选择" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *_Nonnull action) {
- NSLog(@"选择相册");
- [self pickerPictureFromPhotoLibrary];
- }];
- UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"使用相机拍摄" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *_Nonnull action) {
- NSLog(@"请选择相机");
- [self pickerPictureFromCamera];
- }];
- UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
- [alertController addAction:action1];
- [alertController addAction:action2];
- [alertController addAction:action3];
- [self presentViewController:alertController animated:YES completion:nil];
- }
- // 从相册中选择图片
- - (void)pickerPictureFromPhotoLibrary {
- // UIImagePickerController 图片选择控制器
- UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
- // 设置图片选择控制器从哪种资源类型中获取图片
- [pickerVC setSourceType:(UIImagePickerControllerSourceTypePhotoLibrary)];
- // 是否允许编辑获取到的图片
- pickerVC.allowsEditing = YES;
- // 设置代理
- pickerVC.delegate = self;
- [self presentViewController:pickerVC animated:YES completion:nil];
- }
- // 从相机中获取图片
- - (void)pickerPictureFromCamera {
- // 1.先判断设备摄像头是否可用
- BOOL isCan = [UIImagePickerController isCameraDeviceAvailable:(UIImagePickerControllerCameraDeviceRear)];
- if (!isCan) { // 相机不可使用提前结束方法
- NSLog(@"设备不可用,提前结束");
- return;
- }
- // 初始化图片选择视图控制器
- UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
- // 设置获取资源的类型
- pickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
- // 设置是否允许编辑图片
- pickerVC.allowsEditing = YES;
- // 设置代理
- pickerVC.delegate = self;
- [self presentViewController:pickerVC animated:YES completion:nil];
- }
- #pragma mark - UIImagePickerControllerDelegate的代理方法
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info {
- // // info存储的是我们要的图片, 而图片是编辑后要得到的, 所以使用UIImagePickerControllerEditedImage
- //先要把头像上传,若上传成功了,就把这个图片给显示出来
- //先要把头像上传,若上传成功了,就把这个图片给显示出来
- [self dismissViewControllerAnimated:YES completion:^{
- NSString *mediaType = info[UIImagePickerControllerMediaType];
- if ([mediaType isEqualToString:@"public.image"]) {
- NSLog(@"image...");
- //获取图片原图
- UIImage *original = [info objectForKey:UIImagePickerControllerEditedImage];
- //设置压缩的image的尺寸
- CGSize imagesize = original.size;
- imagesize.width = 100;
- imagesize.height = 100;
- //对图片大小进行压缩
- UIImage *newImg = [self imageWithImage:original scaledToSize:imagesize];
- NSData *imageData = UIImageJPEGRepresentation(newImg, 1);
- UIImage *selectImg = [UIImage imageWithData:imageData];
- // NSLog(@"selectImg:%@",selectImg);
- self.workPicView.image = selectImg;
- [self postPicDataWithNetworkingWith:newImg];
- }
- }];
- }
- //用户取消退出picker时候调用
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
- NSLog(@"picker = %@", picker);
- [self dismissViewControllerAnimated:YES completion:^{
- }];
- }
- //对图片尺寸进行压缩--
- - (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
- // Create a graphics image context
- UIGraphicsBeginImageContext(newSize);
- // Tell the old image to draw in this new context, with the desired
- // new size
- [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
- // Get the new image from the context
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- // End the context
- UIGraphicsEndImageContext();
- // Return the new image.
- return newImage;
- }
- #pragma mark -- 上传图片
- - (void)postPicDataWithNetworkingWith:(UIImage *)image {
- [MBProgressHUD showHUDAddedTo:self.view animated:YES];
- [YanCNetWorkManager requestPostWithURLStr:Url_skillphoto(PublicUrl) fileData:image name:@"img" fileName:@"111.png" mimeType:@"image/jpg/png" parameters:nil finish:^(id dataDic) {
- [MBProgressHUD hideHUDForView:self.view animated:YES];
- NSString *issuccess = dataDic[@"msg"];
- if ([issuccess isEqualToString:@"success"]) {
- self.imageStr = [NSString stringWithFormat:@"%@", dataDic[@"img"]];
- } else {
- [MBProgressHUD showInfo:@"请求失败!"];
- }
- } enError:^(NSError *error) {
- [MBProgressHUD hideHUDForView:self.view animated:YES];
- }];
- }
- /*
- {
- img = "upload/photo/20180720/A47B4B53E849FE2F92F90C93FAD4F1C4.jpg";
- msg = success;
- }
- */
- #pragma mark ----- UITextFieldDelegate
- - (void)textFieldDidEndEditing:(UITextField *)textField {
- NSLog(@"textField.text = %@", textField.text);
- }
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
- if (textField.tag == 700) {
- if (![self isValidAboutInputText:textField shouldChangeCharactersInRange:range replacementString:string decimalNumber:2]) {
- return NO;
- }
- }
- return YES;
- }
- //输入框中只能输入数字和小数点,且小数点只能输入一位,参数number 可以设置小数的位数,该函数在-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string调用;
- - (BOOL)isValidAboutInputText:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string decimalNumber:(NSInteger)number {
- NSScanner *scanner = [NSScanner scannerWithString:string];
- NSCharacterSet *numbers;
- NSRange pointRange = [textField.text rangeOfString:@"."];
- if ((pointRange.length > 0) && (pointRange.location < range.location || pointRange.location > range.location + range.length)) {
- numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
- } else {
- numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
- }
- if ([textField.text isEqualToString:@""] && [string isEqualToString:@"."]) {
- return NO;
- }
- short remain = number; //保留 number位小数
- NSString *tempStr = [textField.text stringByAppendingString:string];
- NSUInteger strlen = [tempStr length];
- if (pointRange.length > 0 && pointRange.location > 0) { //判断输入框内是否含有“.”。
- if ([string isEqualToString:@"."]) { //当输入框内已经含有“.”时,如果再输入“.”则被视为无效。
- return NO;
- }
- if (strlen > 0 && (strlen - pointRange.location) > remain + 1) { //当输入框内已经含有“.”,当字符串长度减去小数点前面的字符串长度大于需要要保留的小数点位数,则视当次输入无效。
- return NO;
- }
- }
- NSRange zeroRange = [textField.text rangeOfString:@"0"];
- if (zeroRange.length == 1 && zeroRange.location == 0) { //判断输入框第一个字符是否为“0”
- if (![string isEqualToString:@"0"] && ![string isEqualToString:@"."] && [textField.text length] == 1) { //当输入框只有一个字符并且字符为“0”时,再输入不为“0”或者“.”的字符时,则将此输入替换输入框的这唯一字符。
- textField.text = string;
- return NO;
- } else {
- if (pointRange.length == 0 && pointRange.location > 0) { //当输入框第一个字符为“0”时,并且没有“.”字符时,如果当此输入的字符为“0”,则视当此输入无效。
- if ([string isEqualToString:@"0"]) {
- return NO;
- }
- }
- }
- }
- NSString *buffer;
- if (![scanner scanCharactersFromSet:numbers intoString:&buffer] && ([string length] != 0)) {
- return NO;
- } else {
- return YES;
- }
- }
- @end
|