国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

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

2020-07-26 02:52:13
字體:
供稿:網(wǎng)友

常規(guī)思路:

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

本文思路:

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

展示圖:

代碼:

自定義一個(gè)繼承 UIView 的 CYStarView

屬性:

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

重寫 setter 方法,在 setter 方法中調(diào)用 setNeedsDisplay,會(huì)執(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"http:// 初始化,傳入必要參數(shù)CYStarView *starView = [[CYStarView alloc] initWithFrame:frame numberOfStars:number lengthOfSide:length];// 設(shè)置 clickable,評(píng)論界面設(shè)置為YES,展示界面設(shè)置為NOself.starView.clickable = YES;// // 設(shè)置 completionBlockself.starView.completionBlock = ^(NSInteger currentValue) {  // 點(diǎn)擊后的操作放這里};

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临潭县| 龙胜| 定襄县| 溧水县| 邵东县| 民勤县| 红原县| 乌拉特中旗| 正阳县| 平阴县| 玛纳斯县| 聂荣县| 东辽县| 晋州市| 宁津县| 罗田县| 拉萨市| 冕宁县| 永丰县| 台湾省| 略阳县| 卢氏县| 澄迈县| 鄂州市| 屏东市| 栖霞市| 兴文县| 高要市| 广宁县| 阜康市| 平度市| 纳雍县| 苏尼特左旗| 平凉市| 瑞丽市| 封开县| 澎湖县| 铜陵市| 长武县| 阜宁县| 米易县|