广告
返回顶部
首页 > 资讯 > 移动开发 >iOS实现轮盘动态效果
  • 922
分享到

iOS实现轮盘动态效果

iOS轮盘 2022-05-27 02:05:14 922人浏览 独家记忆
摘要

本文实例为大家分享了iOS实现轮盘动态效果的具体代码,供大家参考,具体内容如下 一个常用的绘图,主要用来打分之类的动画,效果如下。 主要是ioS的绘图和动画,本来想用系统自带动画实

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

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

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

隐式动画的角度考虑下


#import <UIKit/UIKit.h>
 

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

-(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

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

--结束END--

本文标题: iOS实现轮盘动态效果

本文链接: https://www.lsjlt.com/news/30449.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • iOS实现轮盘动态效果
    本文实例为大家分享了iOS实现轮盘动态效果的具体代码,供大家参考,具体内容如下 一个常用的绘图,主要用来打分之类的动画,效果如下。 主要是iOS的绘图和动画,本来想用系统自带动画实...
    99+
    2022-05-27
    iOS 轮盘
  • iOS实现转盘效果
    本文实例为大家分享了iOS实现转盘效果的具体代码,供大家参考,具体内容如下 Demo下载地址: iOS转盘效果 功能:实现了常用的iOS转盘效果,轮盘抽奖效果的实现,转盘可以暂停,旋...
    99+
    2022-05-25
    iOS 转盘
  • iOS简单实现轮播图效果
    本文实例为大家分享了iOS简单实现轮播图效果的具体代码,供大家参考,具体内容如下 平常在开发过程中,首页的轮播图总是少不了,轮播图我们都知道肯定是要使用 UIScrollView ,...
    99+
    2022-11-13
    iOS 轮播图
  • iOS实现3D卡片式轮播效果
    本文实例为大家分享了iOS实现3D卡片式轮播效果的具体代码,供大家参考,具体内容如下 效果: 参考UITableView的UITableViewDataSource和UITable...
    99+
    2022-05-28
    iOS 3D 轮播
  • js实现滑动轮播效果
    本文实例为大家分享了js实现滑动轮播效果的具体代码,供大家参考,具体内容如下 1、构建html样式,代码如下 <div class="banner"> ...
    99+
    2022-11-12
  • iOS实现卡片式滚动效果 iOS实现电影选片效果
    本文实例为大家分享了iOS实现卡片式滚动效果的具体代码,供大家参考,具体内容如下 先来张效果图吧: 直接上源码了: CardScrollView.h #import <U...
    99+
    2022-06-01
    iOS 卡片 滚动
  • iOS实现背景滑动效果
    本文实例为大家分享了iOS实现背景滑动效果的具体代码,供大家参考,具体内容如下 1、在很多APP中,我们都可以看见那些特效绚丽的滑动选项条,那么如何才能够简单,快速的实现那样的效果呢...
    99+
    2022-11-13
  • iOS实现无限滑动效果
    在看到这个标题的时候,相信大家心里肯定会想,无限循环轮播的博客已经满天飞了,好有必要写么。这里我想声明一下,这里的无线滑动,但是数据却不循环。 实现原理 由于业务的需求,需要有大量的...
    99+
    2022-11-13
  • iOS实现图片抖动效果
    本文实例为大家分享了iOS实现图片抖动效果的具体代码,供大家参考,具体内容如下 效果图: 核心代码: // // ViewController.m // 图标抖动 // // ...
    99+
    2022-05-28
    iOS 图片抖动
  • iOS实现垂直滑动条效果
    我们知道在 iOS 开发中,有一个控件经常用到,那就是滑动条(UISlider),可以满足我们滑动取值的需求。但是现在有一个需求,就是需要一个垂直的滑动条,而 UISlider 并不...
    99+
    2022-11-13
  • jquery实现图片自动轮播效果
    本文实例为大家分享了jquery实现图片自动轮播效果的具体代码,供大家参考,具体内容如下 HTML代码如下: <div id="container">         ...
    99+
    2022-11-13
  • iOS实现抖音点赞动画效果
    本文实例为大家分享了iOS实现抖音点赞动画的具体代码,供大家参考,具体内容如下 1. 概述 最近看到抖音点赞爱心的动画效果比较好,出于好奇,自己也研究仿照动画效果写了一个,不喜欢的朋...
    99+
    2022-05-31
    iOS 抖音 点赞
  • iOS实现数字倍数动画效果
    前言 一个简单的利用 透明度和 缩放 实现的 数字倍数动画 效果图: 实现思路 上代码 看比较清晰 // 数字跳动动画 - (void)labelDanceAnimatio...
    99+
    2022-06-05
    ios 倍数 动画
  • iOS实现图片自动切换效果
    本文实例为大家分享了iOS实现图片自动切换的具体代码,供大家参考,具体内容如下 #import "ViewController.h" #define ImageViewCount...
    99+
    2022-05-24
    iOS 图片切换
  • iOS如何实现背景滑动效果
    这篇文章主要介绍了iOS如何实现背景滑动效果,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。具体内容如下第一步、在很多APP中,我们都可以看见那些特效绚丽的滑动选项条,那么如何...
    99+
    2023-06-29
  • iOS的GIF动画效果怎么实现
    本篇文章给大家分享的是有关iOS的GIF动画效果怎么实现,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。GIF在iOS中的使用场景  GIF在iOS中的使用场景有以下三个方面。&...
    99+
    2023-06-04
  • 纯css如何实现轮播图banner自动轮换效果
    这篇文章主要为大家展示了纯css如何实现轮播图banner自动轮换效果,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带大家一起来研究并学习一下“纯css如何实现轮播图banner自动轮换效果”这篇文章吧。css是什么意思cs...
    99+
    2023-06-08
  • iOS实现新年抽奖转盘效果的思路
    临近春节,相信不少app都会加一个新的需求——新年抽奖 不多废话,先上GIF效果图 DEMO链接 跑马灯效果 抽奖效果 实现步骤: 一、跑马灯效果 其实很简单,就是通过以下两...
    99+
    2022-05-24
    iOS 抽奖转盘
  • Vue实现首页banner自动轮播效果
    本文实例为大家分享了Vue实现首页banner自动轮播的具体代码,供大家参考,具体内容如下 效果如图:  ①创建Banner.vue组件,需传入banner数组,可设置轮...
    99+
    2022-11-13
  • jquery实现图片轮播和滑动效果
    本文实例为大家分享了jquery实现图片轮播和滑动效果的具体代码,供大家参考,具体内容如下 实习做了一个简易的图片轮播效果 下图是做出来的效果 源码 html 和 js部分 <...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作