代码之家  ›  专栏  ›  技术社区  ›  frankodwyer

在iphone视图中显示半星评级的简单/快速方法是什么

  •  1
  • frankodwyer  · 技术社区  · 17 年前

    • 相当快(这是表视图中单元格的一部分)

    2 回复  |  直到 17 年前
        1
  •  5
  •   frankodwyer    17 年前

    - (void)drawRect:(CGRect)rect {
        // Drawing code
        CGContextRef context = UIGraphicsGetCurrentContext();
        [textColor set];
        NSString* stars=@"★★★★★";
        rect=self.bounds;
        UIFont *font = [UIFont boldSystemFontOfSize: 16];
        CGSize starSize = [stars sizeWithFont: font];
        rect.size=starSize;
        [@"☆☆☆☆☆" drawInRect:rect withFont: font];
        CGRect clip=rect;
        clip.size.width=clip.size.width*rating/100;
        CGContextClipToRect(context,clip);
        [stars drawInRect:rect withFont:font];
    }
    

    (这些字符串是5个空星号和5个全星号,以防它们只显示在我的mac上。我刚刚在IB中使用“特殊字符”菜单输入了它们)

        2
  •  5
  •   phillipuniverse    15 年前

    我会考虑将两个UIImageView直接层叠在一起。底部图像视图将包含一个由五颗“空”星组成的PNG,顶层为五颗“满”星的PNG。我会根据评级调整顶层的宽度,以暴露下面的空白开始。

    newStarLayerWidth = fullStarLayerWidth * (percentage / 100)
    

    如果速度不够快,我会采取相同的方法,但直接按照所述绘制单元格 here