根据计数重复图像

时间:2017-01-03 10:10:27

标签: ios objective-c xcode

基本上我需要在图片的帮助下显示“评分”,我需要根据数量重复一个图像。就像变量计数为3一样,图像将显示三次。怎么做?

Like this

1 个答案:

答案 0 :(得分:1)

您可以运行循环并将图像作为子视图添加到某个视图中。

-(UIView*)createRatingView :(NSInteger)stars {
     UIView* rating = [[UIView alloc]initWithFrame:CGRectMake(0,0,120,20)]; //set proper frame.. In this example (20*5) + (5*4) where 20 is width of image and 5 is padding between stars

     for (i=1;i<=5;i++) {
         int xPos = (i-1)*(20+5); // assuming 20 is width of image and 5 is padding between 2 stars
         UIImageView* img = [[UIImageView alloc]initWithFrame:CGRectMake(xPos,0,20,20)];
         if (stars<= i) {
             img.image = [UIImage named:@"goldStar"];
         }
         else {
             img.image = [UIImage named:@"grayStar"];
         }
         [rating addSubview : img];
     }
     return rating;
}