电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

iOS實現輪盤動態效果

瀏覽:7日期:2022-09-17 09:44:45

本文實例為大家分享了iOS實現輪盤動態效果的具體代碼,供大家參考,具體內容如下

一個常用的繪圖,主要用來打分之類的動畫,效果如下。

iOS實現輪盤動態效果

主要是iOS的繪圖和動畫,本來想用系統自帶動畫實現呢,發現實現不了,用了其他辦法延時并重繪視圖沒有成功,用了gcd延時,nsthread休眠,performselector delay 都不行。最后用了一個定時器實現類似效果,總感覺不太明智,以后應該考慮下對CALayer和

隱式動畫的角度考慮下

#import <UIKit/UIKit.h> /** * 自定義變量里面以s結尾的表示具體的數值,否則表示的是表示顯示具體內容的標簽,以lbe的表示對內容的說明 比如comments 表示的具體評價內容,comment 表示評價的具體內容,lbecomment 是一個顯示 '評價:'的標簽 */ @interface ScorePlateView : UIView /*速度滿意度*/@property (nonatomic,assign) CGFloat speedValues; /*態度滿意度*/@property (nonatomic,assign) CGFloat altitudeValues; /*把半圓分割的份數*/@property (nonatomic,assign) int precision;/** * 整體評價 */@property (nonatomic,strong) NSString * comments;/** * 滿分是多少分 */@property (nonatomic,assign) CGFloat fullValues;/** * 綜合評分 */@property (nonatomic,assign) CGFloat compreValues;/** * 開始角度 */ @property (nonatomic,assign) CGFloat startAngle;/** * 終止角度 */@property (nonatomic,assign) CGFloat endAngle; //-(void)startAnimation;@end

#import 'ScorePlateView.h' @interface ScorePlateView(){ CGFloat d_speed;//執行動畫時候每個的增量 CGFloat d_altitude; CGFloat d_comp;} @property (nonatomic,strong) UILabel*lbeCompreValue;//綜合分數的標簽 @property (nonatomic,strong) UILabel *compreValue;//綜合分數的具體數值 @property (nonatomic,strong) UILabel * comment;//評價 @property (nonatomic,assign) CGFloat cur_speedV;//當前的值 @property (nonatomic,assign) CGFloat cur_altitudeV;//當前的態度; @property (nonatomic,assign) CGFloat cur_compV;//當前的綜合分數@property (nonatomic,assign) NSTimer * timer; @end @implementation ScorePlateView - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) {self.precision= 50;//這里設置默認值; self.fullValues =5; self.altitudeValues=3.0; self.speedValues=4.0; self.backgroundColor = [UIColor clearColor]; self.startAngle=0.1*M_PI; self.endAngle = 0.9*M_PI; self.comments =@'真是太不可思議了'; self.backgroundColor = [UIColor greenColor]; _cur_compV=0; _cur_speedV=0; _cur_altitudeV=0; } return self;}- (void)drawRect:(CGRect)rect { //1. 畫圓 CGFloat circleMargin = 0; //上下兩個半圓的間距 CGFloat topBottomMargin =20;//這個間距用來顯示服務態度和服務速度那樣標簽內容 CGFloat radius = (self.frame.size.height-circleMargin-2*topBottomMargin)/2;//半徑 //上邊圓的圓心 CGPoint centerTop = CGPointMake(self.frame.size.width/2,self.frame.size.height/2-circleMargin/2); [self drawHalfCircle:centerTop andWithRadius:radius isTop:YES]; //下面圓的圓心 CGPoint centerBottom = CGPointMake(self.frame.size.width/2, self.frame.size.height/2+circleMargin/2); [self drawHalfCircle:centerBottom andWithRadius:radius isTop:NO]; //2. 創建需要的標簽,并在合適的位置繪制內容 UILabel * lbeAltitude = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, topBottomMargin)]; lbeAltitude.text = @'服務速度'; lbeAltitude.textColor = [UIColor whiteColor]; lbeAltitude.textAlignment = NSTextAlignmentCenter; lbeAltitude.font = [UIFont systemFontOfSize:12]; [lbeAltitude drawTextInRect:lbeAltitude.frame]; //服務態度評分 UILabel * lbeSpeed = [[UILabel alloc]initWithFrame:CGRectMake(0, self.frame.size.height-topBottomMargin, self.frame.size.width, topBottomMargin)]; lbeSpeed.text = @'服務態度'; lbeSpeed.textColor = [UIColor whiteColor]; lbeSpeed.textAlignment = NSTextAlignmentCenter; lbeSpeed.font = [UIFont systemFontOfSize:12]; [lbeSpeed drawTextInRect:lbeSpeed.frame]; //繪制綜合評分 NSString *attitudeScore = [NSString stringWithFormat:@'%.2f/%.2f',_cur_altitudeV,self.fullValues]; NSMutableAttributedString* attributeString = [[NSMutableAttributedString alloc]initWithString:attitudeScore]; [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:26] range:NSMakeRange(0, 4)]; [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(4, 3)]; self.compreValue = [[UILabel alloc]initWithFrame:CGRectMake(0, radius-topBottomMargin,self.frame.size.width, 2*topBottomMargin)]; self.compreValue.attributedText = attributeString; self.compreValue.textAlignment = NSTextAlignmentCenter; self.compreValue.textColor = [UIColor whiteColor]; self.compreValue.frame = CGRectMake(0, centerTop.y-topBottomMargin+circleMargin/2, self.frame.size.width, topBottomMargin*2); [self.compreValue drawTextInRect:self.compreValue.frame]; self.lbeCompreValue = [[UILabel alloc]initWithFrame:CGRectMake(0, centerTop.y-radius*0.5, self.frame.size.width, topBottomMargin*2)]; self.lbeCompreValue.text =@'綜合評分'; self.lbeCompreValue.textAlignment = NSTextAlignmentCenter; self.lbeCompreValue.textColor = [UIColor whiteColor]; self.lbeCompreValue.font = [UIFont systemFontOfSize:14]; [self.lbeCompreValue drawTextInRect:self.lbeCompreValue.frame]; //評價內容 self.comment = [[UILabel alloc]initWithFrame:CGRectMake(topBottomMargin+circleMargin+radius/2, CGRectGetMaxY(self.compreValue.frame), radius, topBottomMargin*2)]; self.comment.text =self.comments; self.comment.numberOfLines=0; self.comment.textAlignment = NSTextAlignmentCenter; self.comment.textColor = [UIColor whiteColor]; self.comment.font = [UIFont systemFontOfSize:14]; [self.comment drawTextInRect:self.comment.frame]; }/** * 畫半圓 繪制刻度的時候可以先繪制從圓心的線,最后用一個內圓裁剪的方式,但是如果要求背景隨時變化就局限了,特別是父視圖背景是漸變的,要么重新定義該類,要么把這個類視圖定義為透明,從而透過父視圖背景顏色 透明的背景無法掩蓋從圓心出發的線, * * @param center 圓心坐標 * @param radius 半徑 * @param top 是不是畫上面的圓 */-(void)drawHalfCircle:(CGPoint) center andWithRadius:(CGFloat)radius isTop:(BOOL) top{ //畫上面圓的邊框 CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor); if (top) { CGContextAddArc(ctx, center.x, center.y, radius, -self.startAngle, -self.endAngle, 1); } else { CGContextAddArc(ctx, center.x, center.y, radius,self.startAngle,self.endAngle, 0); } //設置線的寬度 CGContextSetLineWidth(ctx, 1); CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor); CGContextStrokePath(ctx); //繪制上下圓的分割線 CGContextSetLineWidth(ctx, 2);//設置線寬 CGFloat borderValue; if (top) { borderValue=_cur_altitudeV/self.fullValues;//設置邊界值 } else { borderValue =_cur_speedV/self.fullValues; } //實現動畫效果,只能先畫刻度,再畫具體值 for (int i=1; i<self.precision; i++)//畫刻度 {CGContextSetRGBStrokeColor(ctx, 1.0, 1.0, 1.0, 0.5);//設置白色 透明 CGFloat endX,endY,startX,startY;//刻度的長度是這里 7 -2 =5; if (top) { startX = center.x -(radius-10)*cos((double)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle); startY = center.y - (radius-10)*sin((double)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle); endX = center.x - (radius-2)*cos((double)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle);//圓上的點的x坐標 endY = center.y - (radius-2)*sin((double)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle);//圓上的點的y坐標 } else { startX = center.x +(radius-10)*cos((double)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle); startY = center.y + (radius-10)*sin((double)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle); endX = center.x + (radius-2)*cos((double)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle);//圓上的點的x坐標 endY = center.y + (radius-2)*sin((double)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle);//圓上的點的y坐標 } CGContextMoveToPoint(ctx, startX, startY); CGContextAddLineToPoint(ctx, endX, endY); CGContextStrokePath(ctx); } for (int i=1; i<self.precision; i++) {CGFloat curAngle =(double)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle; if ((double)i/(double)self.precision<borderValue) { CGContextSetRGBStrokeColor(ctx, 1.0, 1.0, 1.0, 1);//設置白色 不透明 CGFloat endX,endY,startX,startY;//刻度的長度是這里 7 -2 =5; if (top) {startX = center.x -(radius-10)*cos(curAngle);startY = center.y - (radius-10)*sin(curAngle);endX = center.x - (radius-2)*cos(curAngle);//圓上的點的x坐標endY = center.y - (radius-2)*sin(curAngle);//圓上的點的y坐標 } else {startX = center.x +(radius-10)*cos(curAngle);startY = center.y + (radius-10)*sin(curAngle);endX = center.x + (radius-2)*cos(curAngle);//圓上的點的x坐標endY = center.y + (radius-2)*sin(curAngle);//圓上的點的y坐標 } CGContextMoveToPoint(ctx, startX, startY); CGContextAddLineToPoint(ctx, endX, endY); CGContextStrokePath(ctx); } }}- (void)didMoveToSuperview{ self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(update) userInfo:nil repeats:YES]; d_comp = self.compreValues/20; d_speed= self.speedValues/20; d_altitude=self.speedValues/20;}-(void)update{ _cur_altitudeV+=d_altitude; _cur_speedV+=d_speed; _cur_compV+=d_comp; if (_cur_altitudeV>self.altitudeValues) { _cur_altitudeV =self.altitudeValues; } if (_cur_speedV > self.speedValues) { _cur_speedV=self.speedValues; } if (_cur_compV>self.compreValues) { _cur_compV=self.compreValues; } if ( _cur_compV==self.compreValues&&_cur_speedV==self.speedValues&&_cur_altitudeV ==self.altitudeValues) {[self.timer invalidate]; self.timer = nil; } //self.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1]; [self setNeedsDisplay]; } @end

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: IOS
相關文章:
主站蜘蛛池模板: 蓄电池回收,ups电池后备电源回收,铅酸蓄电池回收,机房电源回收-广州益夫铅酸电池回收公司 | 酶联免疫分析仪-多管旋涡混合仪|混合器-莱普特科学仪器(北京)有限公司 | 分子蒸馏设备(短程分子蒸馏装置)_上海达丰仪器 | 磁力抛光机_磁力研磨机_磁力去毛刺机_精密五金零件抛光设备厂家-冠古科技 | 袋式过滤器,自清洗过滤器,保安过滤器,篮式过滤器,气体过滤器,全自动过滤器,反冲洗过滤器,管道过滤器,无锡驰业环保科技有限公司 | 空调风机,低噪声离心式通风机,不锈钢防爆风机,前倾皮带传动风机,后倾空调风机-山东捷风风机有限公司 | 丁基胶边来料加工,医用活塞边角料加工,异戊二烯橡胶边来料加工-河北盛唐橡胶制品有限公司 | 新疆十佳旅行社_新疆旅游报价_新疆自驾跟团游-新疆中西部国际旅行社 | 超声波成孔成槽质量检测仪-压浆机-桥梁预应力智能张拉设备-上海硕冠检测设备有限公司 | 低合金板|安阳低合金板|河南低合金板|高强度板|桥梁板_安阳润兴 北京租车牌|京牌指标租赁|小客车指标出租 | 暖气片十大品牌厂家_铜铝复合暖气片厂家_暖气片什么牌子好_欣鑫达散热器 | 电位器_轻触开关_USB连接器_广东精密龙电子科技有限公司 | 砍排机-锯骨机-冻肉切丁机-熟肉切片机-预制菜生产线一站式服务厂商 - 广州市祥九瑞盈机械设备有限公司 | 雨水收集系统厂家-雨水收集利用-模块雨水收集池-徐州博智环保科技有限公司 | 首页-恒温恒湿试验箱_恒温恒湿箱_高低温试验箱_高低温交变湿热试验箱_苏州正合 | 泰兴市热钻机械有限公司-热熔钻孔机-数控热熔钻-热熔钻孔攻牙一体机 | 机械立体车库租赁_立体停车设备出租_智能停车场厂家_春华起重 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | 无锡市珂妮日用化妆品有限公司|珂妮日化官网|洗手液厂家 | 搪瓷反应釜厂家,淄博搪瓷反应釜-淄博卓耀 | 罐体电伴热工程-消防管道电伴热带厂家-山东沃安电气 | 水成膜泡沫灭火剂_氟蛋白泡沫液_河南新乡骏华消防科技厂家 | 防火卷帘门价格-聊城一维工贸特级防火卷帘门厂家▲ | 校园文化空间设计-数字化|中医文化空间设计-党建|法治廉政主题文化空间施工-山东锐尚文化传播公司 | 臻知网大型互动问答社区-你的问题将在这里得到解答!-无锡据风网络科技有限公司 | 工业洗衣机_工业洗涤设备_上海力净工业洗衣机厂家-洗涤设备首页 bkzzy在职研究生网 - 在职研究生招生信息咨询平台 | 成人纸尿裤,成人尿不湿,成人护理垫-山东康舜日用品有限公司 | 净水器代理,净水器招商,净水器加盟-FineSky德国法兹全屋净水 | 东莞猎头公司_深圳猎头公司_广州猎头公司-广东万诚猎头提供企业中高端人才招聘服务 | 广东恩亿梯电源有限公司【官网】_UPS不间断电源|EPS应急电源|模块化机房|电动汽车充电桩_UPS电源厂家(恩亿梯UPS电源,UPS不间断电源,不间断电源UPS) | 温州富欧金属封头-不锈钢封头厂家| 台湾阳明固态继电器-奥托尼克斯光电传感器-接近开关-温控器-光纤传感器-编码器一级代理商江苏用之宜电气 | 沈阳液压泵_沈阳液压阀_沈阳液压站-沈阳海德太科液压设备有限公司 | 多米诺-多米诺世界纪录团队-多米诺世界-多米诺团队培训-多米诺公关活动-多米诺创意广告-多米诺大型表演-多米诺专业赛事 | 工业铝型材-铝合金电机壳-铝排-气动执行器-山东永恒能源集团有限公司 | 千淘酒店差旅平台-中国第一家针对TMC行业的酒店资源供应平台 | 武汉高温老化房,恒温恒湿试验箱,冷热冲击试验箱-武汉安德信检测设备有限公司 | 电缆故障测试仪_电缆故障定位仪_探测仪_检测仪器_陕西意联电气厂家 | 岩棉板|岩棉复合板|聚氨酯夹芯板|岩棉夹芯板|彩钢夹芯板-江苏恒海钢结构 | vr安全体验馆|交通安全|工地安全|禁毒|消防|安全教育体验馆|安全体验教室-贝森德(深圳)科技 | 粘度计,数显粘度计,指针旋转粘度计 |