iOS发布商品评价(文字,图片,星级)原创
金蝶云社区-honey缘木鱼
honey缘木鱼
4人赞赏了该文章 1,225次浏览 未经作者许可,禁止转载编辑于2019年05月29日 15:39:46

设计如下图:

屏幕快照 2019-05-29 下午2.16.44.png

设计需求 :

       1. 评论内容不能超过150个字。

       2.上传图片不能超过4张,点击可看大图,可删除图片。

       3. 根据用户星级个数对用户满意级别做出评价。


一.评论文字(UITextView)

1. 因为UITextView没有placeholder,所以UIlabel控制默认文字的显示

//添加addPlaceholder

-(void)addPlaceholderLabel{

    self.placeholderLabel=[[UILabel alloc] initWithFrame:CGRectMake(5, -5, kScreenWidth-50, 44)];

    self.placeholderLabel.text=@"";

    self.placeholderLabel.textColor=titleColor ;

    self.placeholderLabel.font=[UIFont systemFontOfSize:titleFont];

    [self addSubview:self.placeholderLabel];

}

- (void)setPlaceholder:(NSString *)placeholder {

    _placeholder=placeholder;

    self.placeholderLabel.text=_placeholder;

}

2. 控制文字输入的最大长度

- (void)textViewDidChangeText:(NSNotification *)notification

{

   UITextView *textView = (UITextView *)notification.object;

    NSString *toBeString = textView.text;

    if([IHUtility isBlankString:textView.text]){

         self.placeholderLabel.hidden = NO;

    }else

    {

         self.placeholderLabel.hidden = YES;

    }

    // 获取键盘输入模式

    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage];

    // 中文输入的时候,可能有markedText(高亮选择的文字),需要判断这种状态

    // zh-Hans表示简体中文输入, 包括简体拼音,健体五笔,简体手写

    if ([lang isEqualToString:@"zh-Hans"]) {

        UITextRange *selectedRange = [textView markedTextRange];

        //获取高亮选择部分

        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];

        // 没有高亮选择的字,表明输入结束,则对已输入的文字进行字数统计和限制

        if (!position) {

            if (toBeString.length > [_kMaxLength intValue]) {

                // 截取子串

                textView.text = [toBeString substringToIndex: [_kMaxLength intValue]];

            }

        } else { // 有高亮选择的字符串,则暂不对文字进行统计和限制

        }

    } else {

        // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况

        if (toBeString.length >  [_kMaxLength intValue]) {

            // 截取子串

            textView.text = [toBeString substringToIndex: [_kMaxLength intValue]];

        }

    }

}

3.键盘弹出有完成按钮供用户选择


-(void)hideKeyBoard:(id)sender{

    UIView *v=[self viewWithTag:1001];

    v.origin=CGPointMake(0, kScreenHeight);

    [self resignFirstResponder];

    NSLog(@"输入:%@",self.text);

    if(![IHUtility isBlankString:self.text]&&![self isEmpty:self.text]){

        if (self.delegateBtn != nil && [self.delegateBtn respondsToSelector:@selector(didClickFinishBtn:)]) {

            [self.delegateBtn didClickFinishBtn:self.text ];

            }

    }

}

二.上传图片

1. 上传图片最多4张,用UICollectionViewCell

屏幕快照 2019-05-29 下午2.58.02.png

TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:self.maxImageCount - self.selectIndexPath.item delegate:self];

 [self presentViewController:imagePickerVc animated:YES completion:nil];


 if (self.dataArr.count > self.maxImageCount) {

        NSRange range = NSMakeRange(self.maxImageCount, self.dataArr.count - self.maxImageCount);

        [self.dataArr removeObjectsInRange:range];

    }

    [self.collectionView reloadData];


2.大图浏览,删除

 SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];

 browser.currentImageIndex = indexPath.row ;

 browser.sourceImagesContainerView = self.collectionView;

 //NSArray *pictures = [self.model.contentPicture componentsSeparatedByString:@","];

 browser.flag = self.flag;

  browser.imageCount = self.dataArr.count;

  browser.delegate = self;

  [browser show];


// 删除照片

- (void)delectPaicture:(NSNotification *)notification{

    NSDictionary *dict = notification.object;

    NSInteger currentIndex = [[dict valueForKey:@"index"] integerValue];

    if (self.dataArr.count > 0) {

        [self.dataArr removeObjectAtIndex:currentIndex];

    }

    [self.collectionView reloadData];

    [self resetHeight];

}


3.用户禁止访问时

 NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"];

   if (!appName) appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleName"];

  _tipLable.text = [NSString stringWithFormat:@"请在%@的\"设置-隐私-照片\"选项中,\r允许%@访问你的手机相册。",[UIDevice currentDevice].model,appName];

  [self.view addSubview:_tipLable];


三.星级评价

封装一个星级评价的view,代理或Block都可获得当前用户选择的星星个数。

/**

 *通过代理的方法获取当前评分数currentScore

 */

-(instancetype)initWithFrame:(CGRect)frame numberOfStars:(NSInteger)numberOfStars currentStars:(NSInteger)currentStars rateStyle:(StarRateViewStyle)rateStyle isAnination:(BOOL)isAnimation delegate:(id)delegate;

/**

 *通过Block传值的方法获取当前评分数currentScore

 */

-(instancetype)initWithFrame:(CGRect)frame numberOfStars:(NSInteger)numberOfStars currentStars:(NSInteger)currentStars rateStyle:(StarRateViewStyle)rateStyle isAnination:(BOOL)isAnimation finish:(finishBlock)finish;


1.设置星星数量

- (void)createStarsContentView:(UIImage *)starImage starRate:(CGFloat)starRate {

    

    if (self.numberOfStars == 0) {

        return;

    }

    

    NSInteger index = (NSInteger)floor(currentStarRate);

    CGFloat w = (self.starSize.width + self.spacingBetweenStars) * index + (currentStarRate - index) * self.starSize.width;

    CGFloat x = (CGRectGetWidth(self.bounds) - [self sizeForNumberOfStar:self.numberOfStars].width) * 0.5;

    CGFloat y = (CGRectGetHeight(self.bounds) - [self sizeForNumberOfStar:self.numberOfStars].height) * 0.5;

    CGFloat h = self.starSize.height;

    CGRect frame = CGRectMake(x, y, w, h);

    UIView *starsContentView = [[UIView alloc] initWithFrame:frame];

    starsContentView.clipsToBounds = YES;

    [self addSubview:starsContentView];

    

    for (int i = 0; i < self.numberOfStars; i++) {

        

        UIImageView *imageView = [[UIImageView alloc] initWithImage:starImage];

        imageView.frame = CGRectMake((self.starSize.width + self.spacingBetweenStars) * i, 0, self.starSize.width, self.starSize.height);

        imageView.contentMode = UIViewContentModeScaleAspectFit;

        [starsContentView addSubview:imageView];

    }

    

    [self.starsContentViews addObject:starsContentView];

}

2.用户点击事件

#pragma mark - event

//一根手指或多根手指触摸开始

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

        UITouch *touch = [touches anyObject];

        UIView *view = touch.view;

        if (view != self) {

            CGPoint point = [touch locationInView:view];

            [self setupScoreWithOffsetX:point.x];

        }

}

//一根手指或多根手指在屏幕上移动(随着手指的移动,会触发该事件)

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

        UITouch *touch = [touches anyObject];

        UIView *view = touch.view;

        if (view != self && [self.starsContentViews containsObject:view]) {

            CGPoint point = [touch locationInView:view];

            [self setupScoreWithOffsetX:point.x];

        }

}

3.计算评分值

 NSInteger index = offsetX / (self.starSize.width + self.spacingBetweenStars);

    CGFloat mathOffsetX =  (index + 1) * self.starSize.width + index * self.spacingBetweenStars;

    CGFloat score = (offsetX - index * self.spacingBetweenStars)/(self.starSize.width);

    if (offsetX > mathOffsetX) {

        score = index + 1;

    }

 if(self.style==StarRateViewStyleWholeStar){

        self.currentStarRate =ceil(score);

    }else

    {

      self.currentStarRate =  round(score) > score ? round(score) : (ceil(score)-0.5);

    }


  if (self.complete) {

        _complete(self.currentStarRate);

    }

    if ([self.delegate respondsToSelector:@selector(starRateView:didSelecteStarAtStarRate:)]) {

        [self.delegate starRateView:self didSelecteStarAtStarRate:self.currentStarRate];

    }

4.具体使用

#pragma StarRateView代理方法

- (void)starRateView:(StarRateView *)starRateView didSelecteStarAtStarRate:(CGFloat)currentScore {

    for(int i=0;i<_starArray.count;i++){

        StarRateView *starView = _starArray[i];

        if(starRateView.tag==starView.tag){

            [_lastStarRateDict setObject:[NSString stringWithFormat:@"%d",(int)currentScore] forKey:[NSString stringWithFormat:@"%d",(int)starRateView.tag]];

            UILabel *infoLabel = _infoArray[i];

            if(currentScore>4){

                infoLabel.text = @"很满意";

            }else if (currentScore>1){

                infoLabel.text = @"满意";

            }else

            {

                infoLabel.text = @"一般";

            }

        }

    }

}


四.下载地址

五.使用介绍

1559115242110.jpg


1.初始化YJTextView

-(YJTextView*)textView{

    if(!_textView){

        _textView = [[YJTextView alloc]initWithFrame:CGRectMake(15, 100, kScreenWidth-30, 125)];

        _textView.backgroundColor = RGBA(245, 245, 245, 1);

        _textView.layer.masksToBounds  = YES;

        _textView.layer.cornerRadius = 8;

        _textView.kMaxLength = @150;

        _textView.placeholder = @"说说商家到亮点与不足吧!";

        _textView.placeholderLabel.textColor = RGBA(153, 153, 153, 1);

    }

    return _textView;

}


2. 上传图片

CXXChooseImageViewController *vc = [[CXXChooseImageViewController alloc] init];

    vc.view.backgroundColor=[UIColor clearColor];

    vc.delegate = self;

    self.vc = vc;

    [self addChildViewController:vc];

    

    CGFloat photoWith =( kScreenWidth-55)/4;

    [vc setOrigin:CGPointMake(7,_textView.bottom) ItemSize:CGSizeMake(photoWith, photoWith) rowCount:4];

    [self.view insertSubview:vc.view atIndex:[self.view subviews].count];

    vc.maxImageCount = 4;

//代理的方法代表上传的图片值

-(void)pickImages:(NSArray *)imageArr andAllPickPitures:(NSArray *)allPitures

{

    _allPickPitures = [[NSMutableArray alloc]initWithArray:allPitures];

}


3.初始化星级view

StarRateView *starRateView = [[StarRateView alloc]initWithFrame:CGRectMake(textLabel.right+10, _textView.bottom+90+50+i*(24+20), 200, 10) numberOfStars:5 currentStars:0  rateStyle:0 isAnination:YES delegate:self];

  starRateView.tag = 100+i;

 [self.view addSubview:starRateView];


4.获得最终的数据

//去发布

-(void)gotoPublic{

    NSLog(@"输入评论:%@",self.textView.text);

    NSLog(@"上传的图片%@",_allPickPitures);

     NSLog(@"第三者的评分%@",_lastStarRateDict);

}




本篇独发金蝶云社区

             


图标赞 4
4人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!