美文网首页我们就爱程序媛iOS Developer
【iOS】0行代码实现评分星星视图

【iOS】0行代码实现评分星星视图

作者: 清無 | 来源:发表于2017-07-21 12:41 被阅读109次
最终效果
要求:
  • Platform: iOS7.0+
  • Language: Objective-C
  • Editor: Xcode6.0+
实现
  • 思路
    UIPogressView + UIImage

总得来说,就利用progressView的两个属性:_progressImage【顶图】和_trackImage【底图】实现不同进度值下的评分效果。

  • 属性设置


    属性设置
  • 交互逻辑代码

注意:_stepValue是指滑动过程中的最小步进值,0.5表示半颗星,1表示一颗星,要求0 < _stepValue <= 1。

-(void)setProgressImage:(UIImage *)progressImage{
    super.progressImage = [self configureImage:progressImage];
}

-(void)setTrackImage:(UIImage *)trackImage{
    super.trackImage = [self configureImage: trackImage];
}

// 重新计算设置图,使其符合平铺要求
- (UIImage*)configureImage:(UIImage*)image
{
    CGFloat width = self.bounds.size.width / 5;
    CGSize size = CGSizeMake(width, width);
    
    UIGraphicsBeginImageContextWithOptions(size, false, image.scale);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return [newImage resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeTile];
}

// 设置progressView的进度
-(void)updateProgressWithTouches: (NSSet<UITouch *> *)touches isMoved: (BOOL)isMoved{
    UITouch *touch = touches.anyObject;
    CGPoint loc = [touch locationInView:_progressView];
    
    float value = loc.x / _progressView.bounds.size.width;
    if (value < 0) {
        return;
    }
    float progress = (_stepValue == 0) ? value : ceil(value * 5 / _stepValue) * _stepValue / 5;
    progress = MIN(progress, 1);

    _progressView.progress = progress;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self updateProgressWithTouches:touches isMoved:false];
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self updateProgressWithTouches:touches isMoved:false];
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self updateProgressWithTouches:touches isMoved:true];
}

-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self updateProgressWithTouches:touches isMoved:false];
}
  • 注意

在设置NSLayout时,设置progressView的高度要和icon_star保持一致,宽度为图片的倍数值,否则会显示错乱。

github

https://github.com/BackWorld/HYStarView

本文只是给读者展示了一种独特的最简单的实现方式,如果对你有帮助,别忘了点个❤️哦。

相关文章

  • 【iOS】0行代码实现评分星星视图

    要求: Platform: iOS7.0+ Language: Objective-C Editor: Xcode...

  • iOS 0行代码实现 TableView 无数据时展示占位视图

    iOS 0行代码实现 TableView 无数据时展示占位视图 iOS 0行代码实现 TableView 无数据时...

  • 评分功能

    iOS系统中没有自带的星星评分功能,需要自己封装。星星评分功能是整个工程的一个需求,但是代码的封装则是一个代码功底...

  • 封装星星评分视图

    星星评分的核心是放置两张星星视图,一张灰色,一张黄色,灰色在下,黄色在上,默认平铺五颗灰色和黄色星星,然后从传入的...

  • 星星评分

    星星评分 我们很多时候 为了 UI界面的 简便 美观 ,时常用到 星级评分 1.实现思路 2.代码实现 .h 文件...

  • 使用paintcode 制作一个星星评分视图

    之前有个朋友有个需求,需要做一个评分视图,视图由5颗星星构成,根据评分,星星对应比例填充好颜色,效果如下: 刚好在...

  • iOS星级评价的两种实现方式

    如何在 iOS 应用上用自定义视图实现星级评分功能呢?最近研究学习了两个实现方法,现总结如下: 一、实现方式一 参...

  • 一个星星评分的简单实现

    NAStarView.h NAStarView.m 使用 附 星星评价视图 小数评分

  • iOS-评分星星实现小技巧

    其实评分的星星实现不是很难,实现的方法很多,有用Quartz2D绘制的,有用self.backgroundColo...

  • 实现评分星星by Swift

    星星评分,支持手势滑动,支持非满星。大致原理是使用UIImageView排列来实现,加上手势,就完成这个功能了,封...

网友评论

    本文标题:【iOS】0行代码实现评分星星视图

    本文链接:https://www.haomeiwen.com/subject/vlnskxtx.html