iOS上下文实现评价星星

coxb8788 8年前
   <h3>常规思路:</h3>    <p>创建两个 view,通过 for 循环创建 imageView,未点亮星星视图在下、点亮星星视图在上重合在一起,当用户点击视图时,通过改变点亮星星视图的 width 实现功能</p>    <h3>本文思路:</h3>    <p>直接重写 drawrect 方法,在 drawrect 用 drawimage 画出星星,根据 currentValue 画出不同类型的星星,当用户点击视图时,改变 currentValue,并根据改变后的 currentValue 重新画出星星。</p>    <h3>展示图:</h3>    <p><img src="https://simg.open-open.com/show/ce1448858097a349735b9ec2394c2860.gif"></p>    <p>展示图</p>    <h3>代码:</h3>    <p>自定义一个继承 UIView 的 CYStarView</p>    <p>属性:</p>    <pre>  <code class="language-objectivec">/** 完成后执行的block */  @property (copy, nonatomic) void(^completionBlock)(NSInteger);  /** 是否可以点击 */  @property (assign, nonatomic) BOOL clickable;  /** 星星个数 */  @property (assign, nonatomic) NSInteger numberOfStars;  /** 星星边长 */  @property (assign, nonatomic) CGFloat lengthOfSide;  /** 评价值 */  @property (assign, nonatomic) NSInteger currentValue;  /** 星星间隔 */  @property (assign, nonatomic) CGFloat spacing;</code></pre>    <p>重写 setter 方法,在 setter 方法中调用 setNeedsDisplay,会执行 drawrect:</p>    <pre>  <code class="language-objectivec">- (void)setLengthOfSide:(CGFloat)lengthOfSide {        // 超过控件高度      if (lengthOfSide > self.frame.size.height) {          lengthOfSide = self.frame.size.height;      }        // 超过控件宽度      if (lengthOfSide > self.frame.size.width / _numberOfStars) {          lengthOfSide = self.frame.size.width / _numberOfStars;      }        _lengthOfSide = lengthOfSide;      _spacing = (self.frame.size.width - lengthOfSide * _numberOfStars) / _numberOfStars;        [self setNeedsDisplay];  }</code></pre>    <p>在 drawrect 中画星星:</p>    <pre>  <code class="language-objectivec">- (void)drawRect:(CGRect)rect {        UIImage *lightImage = [UIImage imageNamed:@"star_light"];      UIImage *darkImage = [UIImage imageNamed:@"star_dark"];        // 获取当前上下文      CGContextRef context = UIGraphicsGetCurrentContext();        for (int i = 0; i < self.numberOfStars; i ++) {          // 根据 currentValue 选择是画亮的还是暗的星星          UIImage *image = i >= self.currentValue ? darkImage : lightImage;          CGRect imageRect = CGRectMake(self.spacing / 2 + (self.lengthOfSide + self.spacing) * i, (self.frame.size.height - self.lengthOfSide) / 2, self.lengthOfSide, self.lengthOfSide);            CGContextSaveGState(context);            // 坐标系Y轴是相反的,进行翻转          CGContextScaleCTM(context, 1.0, - 1.0);          CGContextTranslateCTM(context, 0, - rect.origin.y * 2 - rect.size.height);            CGContextDrawImage(context, imageRect, image.CGImage);          CGContextRestoreGState(context);      }  }</code></pre>    <p>关于坐标系相反而产生的翻转的问题,可以阅读: <a href="/misc/goto?guid=4959746375815662612" rel="nofollow,noindex">CGContextDrawImage使用和分析</a></p>    <h3>使用:</h3>    <p>在要使用的控制器中:</p>    <pre>  <code class="language-objectivec">#import "CYStarView.h"</code></pre>    <pre>  <code class="language-objectivec">// 初始化,传入必要参数  CYStarView *starView = [[CYStarView alloc] initWithFrame:frame numberOfStars:number lengthOfSide:length];  // 设置 clickable,评论界面设置为YES,展示界面设置为NO  self.starView.clickable = YES;  //   // 设置 completionBlock  self.starView.completionBlock = ^(NSInteger currentValue) {      // 点击后的操作放这里  };</code></pre>    <p> </p>    <p>来自:http://www.jianshu.com/p/b87b08b0c7fd</p>    <p> </p>