热门标签 | HotTags
当前位置:  开发笔记 > IOS > 正文

iOS实现轮盘动态效果

这篇文章主要为大家详细介绍了iOS实现轮盘动态效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了iOS实现轮盘动态效果的具体代码,供大家参考,具体内容如下

一个常用的绘图,主要用来打分之类的动画,效果如下。

主要是iOS的绘图和动画,本来想用系统自带动画实现呢,发现实现不了,用了其他办法延时并重绘视图没有成功,用了gcd延时,nsthread休眠,performselector delay 都不行。最后用了一个定时器实现类似效果,总感觉不太明智,以后应该考虑下对CALayer和

隐式动画的角度考虑下

#import 
 
/**
 *  自定义变量里面以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; iself.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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • 打开文件是由参数-filename指定标识文件,将其与流对象相关联,以便对文件内容执行输入/输出操作。参数-mode指定打开模式。 ... [详细]
  • EL&&JSTL
    EL表达式概念ExpressionLanguage表达式语言。作用替换和简化jsp页面中java代码的编写语法${表达式}注意jsp默认支持el表达式的。如果要忽略el表达式可以使 ... [详细]
  • 文档地址:https:github.comaxiosaxiosaxios理解和使用1.请求配置1{2请求服务器的URL3url:'user',45method创 ... [详细]
  • http:nickycc.lofter.compost23e2a6_17d6a07http:blog.csdn.netl25000articledetails46842013htt ... [详细]
  • Spring WebFlux开门迎客,却来了一位特殊客人
    话说SpringWebFlux已经出现有一段时间了,但是知道他的人并不是很多。这让他很是闷闷不乐。还有更惨的是,那些敢于吃螃蟹的人在尝试了他之后,有的竟把代码重新改回到Spring ... [详细]
  • hrbust OJ13哥的机器人(利用叉乘判断拐向)
    ViewCode**利用叉乘判断拐向*auther:Try86*#include#includeusingnamespacestd;ch ... [详细]
  • 1.亥姆霍兹线圈:用于制造一个均匀磁场并垂直于电子运行轨道两个并列的线圈2.LR回路:基尔霍夫定律不成立,因为有自感存在cos2wt或者sin2wt的时间平均值是12思考: ... [详细]
  • 一、向Unity和Android问好欢迎来到手机游戏开发的精彩世界。无论你是还在寻找合适 ... [详细]
  • 树状数组总结——详解(单点/区间查询, 单点/区间修改, 逆序对)
    http:blog.csdn.netwzw1376124061articledetails731882382017-06-1317:2464人阅读评论(0)收藏举报版权声明:!!! ... [详细]
  • 实验十:指针
    #includevoidsplitfloat(floatx,int*intpart,float*fracpart);intmain(void){intintpa ... [详细]
  • OpenCV实现轮廓外接多边形
    这篇文章主要为大家详细介绍了OpenCV实现轮廓外接多边形,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的 ... [详细]
  • 写给前端同学的C++入门教程(一):概述和环境搭建
    说明:本人是前端er,因为最近对UE4(一个游戏开发引擎)产生了兴趣,而这个引擎源开发游戏时需要用到C++,所以就开始入坑C++了。现将自己学习C++的笔记整理并分享出来,以便一些 ... [详细]
  • C++学习之Visual Studio安装以及首次使用
    一、安装VisualStudio首先下载VisualStudio链接:http:pan.baidu.coms1pLhJt0Z密码:uqyc将.ios文件解压得到以下文件:点击vs_ ... [详细]
  • 自己的macbookPro只有128G的硬盘,存储空间经常不足,终于找到了个大文件,,UsersusrnameLibraryContainerscom.tencent.qqData ... [详细]
  • 用uniapp和springboot做出的高效记忆小程序,技术点总结
    临时起意老早前就听说过一些高效记忆的方法,其中听的最多的就是艾宾浩斯记忆法和费曼学习法。恰好赶上过年放假,就在想除了吃吃吃之外,还能干点什么。本来想学习理财的知识,一看概念还真不少 ... [详细]
author-avatar
希瑟小姐
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有