最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS上下文實(shí)現(xiàn)評價星星示例代碼

 更新時間:2017年03月29日 15:55:58   作者:李長友同學(xué)  
這篇文章主要介紹了iOS上下文實(shí)現(xiàn)評價星星的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

常規(guī)思路:

創(chuàng)建兩個 view,通過 for 循環(huán)創(chuàng)建 imageView,未點(diǎn)亮星星視圖在下、點(diǎn)亮星星視圖在上重合在一起,當(dāng)用戶點(diǎn)擊視圖時,通過改變點(diǎn)亮星星視圖的 width 實(shí)現(xiàn)功能

本文思路:

直接重寫 drawrect 方法,在 drawrect 用 drawimage 畫出星星,根據(jù) currentValue 畫出不同類型的星星,當(dāng)用戶點(diǎn)擊視圖時,改變 currentValue,并根據(jù)改變后的 currentValue 重新畫出星星。

展示圖:

代碼:

自定義一個繼承 UIView 的 CYStarView

屬性:

/** 完成后執(zhí)行的block */
@property (copy, nonatomic) void(^completionBlock)(NSInteger);
/** 是否可以點(diǎn)擊 */
@property (assign, nonatomic) BOOL clickable;
/** 星星個數(shù) */
@property (assign, nonatomic) NSInteger numberOfStars;
/** 星星邊長 */
@property (assign, nonatomic) CGFloat lengthOfSide;
/** 評價值 */
@property (assign, nonatomic) NSInteger currentValue;
/** 星星間隔 */
@property (assign, nonatomic) CGFloat spacing;

重寫 setter 方法,在 setter 方法中調(diào)用 setNeedsDisplay,會執(zhí)行 drawrect:

- (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];
}

在 drawrect 中畫星星:

- (void)drawRect:(CGRect)rect {

  UIImage *lightImage = [UIImage imageNamed:@"star_light"];
  UIImage *darkImage = [UIImage imageNamed:@"star_dark"];

  // 獲取當(dāng)前上下文
  CGContextRef context = UIGraphicsGetCurrentContext();

  for (int i = 0; i < self.numberOfStars; i ++) {
    // 根據(jù) 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);

    // 坐標(biāo)系Y軸是相反的,進(jìn)行翻轉(zhuǎn)
    CGContextScaleCTM(context, 1.0, - 1.0);
    CGContextTranslateCTM(context, 0, - rect.origin.y * 2 - rect.size.height);

    CGContextDrawImage(context, imageRect, image.CGImage);
    CGContextRestoreGState(context);
  }
}

使用:

在要使用的控制器中:

#import "CYStarView.h"
// 初始化,傳入必要參數(shù)
CYStarView *starView = [[CYStarView alloc] initWithFrame:frame numberOfStars:number lengthOfSide:length];
// 設(shè)置 clickable,評論界面設(shè)置為YES,展示界面設(shè)置為NO
self.starView.clickable = YES;
// 
// 設(shè)置 completionBlock
self.starView.completionBlock = ^(NSInteger currentValue) {
  // 點(diǎn)擊后的操作放這里
};

項(xiàng)目地址:點(diǎn)我點(diǎn)我!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

阿拉善右旗| 肃北| 湖州市| 江永县| 河源市| 化州市| 长顺县| 嘉善县| 灵山县| 连云港市| 盈江县| 漳浦县| 岳阳市| 莲花县| 磐石市| 北安市| 嘉荫县| 上栗县| 同江市| 高平市| 曲阳县| 边坝县| 栾城县| 东兰县| 富顺县| 花莲市| 大兴区| 汝州市| 安岳县| 昌黎县| 荔浦县| 东平县| 安顺市| 西乡县| 祥云县| 正定县| 盐亭县| 青龙| 孟连| 娱乐| 六枝特区|