博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
雷达扫描动画
阅读量:6504 次
发布时间:2019-06-24

本文共 5447 字,大约阅读时间需要 18 分钟。

 

 

 

#import "ViewController.h"#import "RadarView.h"@interface ViewController ()@property (nonatomic, strong) RadarView *radarView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        self.view.backgroundColor = [UIColor blackColor];    self.radarView = [[RadarView alloc]initWithFrame:CGRectMake(0, 0, 370, 370)];    self.radarView.center = self.view.center;    [self.view addSubview:self.radarView];    [self.radarView radarScan];    // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

 

RadarView.h  文件
////  RadarView.h//  ScanAnimation//#import 
#import "RadarIndicationView.h"@interface RadarView : UIView@property (nonatomic, strong)RadarIndicationView *radarIndicationView;-(void) radarScan;@end
RadarView.m  文件
////  RadarView.m//  ScanAnimation//#import "RadarView.h"@implementation RadarView-(instancetype)initWithFrame:(CGRect)frame{    if ([super initWithFrame:frame]) {        [self addSubview:self.radarIndicationView];    }    return self;}-(void) drawRect:(CGRect)rect{    [super drawRect:rect];    CGContextRef context = UIGraphicsGetCurrentContext();    [self drawCircle:context];}- (RadarIndicationView *)radarIndicationView{    if (!_radarIndicationView) {        _radarIndicationView = [[RadarIndicationView alloc]initWithFrame:CGRectMake(0, 0, 340, CGRectGetHeight(self.frame))];        _radarIndicationView.center = self.center;    }        return _radarIndicationView;    }- (void)drawCircle:(CGContextRef )context{    //将坐标轴移动到视图中心    CGContextTranslateCTM(context, CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);        CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);    CGContextSetLineWidth(context, 2);        //画圆弧    for (int i = 0; i <4; i++) {        CGContextMoveToPoint(context, 50+i*40, 0);        CGContextAddArc(context, 0, 0, 50+i*40, 0,  M_PI*2, 0);    }    CGContextStrokePath(context);        //画十字坐标    CGContextMoveToPoint(context, -170, 0);    CGContextAddLineToPoint(context, 170, 0);        CGContextMoveToPoint(context, 0, -170);    CGContextAddLineToPoint(context, 0, 170);        CGContextStrokePath(context);}//开始扫描-(void) radarScan{    [self.radarIndicationView start];}

 

RadarIndicationView.h  文件
////  RadarIndicationView.h//  ScanAnimation//#import 
@interface RadarIndicationView : UIView-(void) start;-(void) stop;@end

 

RadarIndicationView.m 文件
////  RadarIndicationView.m//  ScanAnimation//#import "RadarIndicationView.h"@implementation RadarIndicationView-(instancetype)initWithFrame:(CGRect)frame{    if ([super initWithFrame:frame]) {        self.backgroundColor = [UIColor clearColor];    }    return self;    }-(void) drawRect:(CGRect)rect{    [super drawRect:rect];    CGContextRef context = UIGraphicsGetCurrentContext();    [self drawArc:context];}//画大扇形-(void) drawArc:(CGContextRef) context{    //将坐标轴移到视图中心    CGContextTranslateCTM(context, CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);    //设置扇形半径        CGFloat radius = CGRectGetWidth(self.frame)/2.0;    CGContextSetLineWidth(context, 0.1);    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);        CGFloat single = M_PI/180/2.0;//画图时每次旋转的度数    CGFloat colorAlpha = M_PI_2/90/2.0;    CGFloat x = 0.0;    for (int i = 0; i < 90*2; i++) {        //画小扇形        CGContextMoveToPoint(context, 0, 0);        CGContextAddArc(context, 0, 0, radius, 0, -single, 1);                //设置填充颜色以及透明度        x = x + colorAlpha;        CGFloat alpha = sin(1-x);        UIColor *color = [UIColor colorWithRed:41/255 green:253/255 blue:47/255 alpha:alpha];                [color setFill];                CGContextFillPath(context);        CGContextDrawPath(context, kCGPathFillStroke);//绘制路径                //逆时针旋转坐标        CGContextRotateCTM(context, -single);            }    }- (void) start{    [self setNeedsDisplay];        CABasicAnimation *rotationAnimation;    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];    rotationAnimation.toValue = @(2*M_PI);    rotationAnimation.duration = 360/60;    rotationAnimation.repeatCount = NSIntegerMax;    [self.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];    }-(void)stop{    [self.layer removeAnimationForKey:@"rotationAnimation"];}-(void)rectangularCoordinates{    //画x    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextSetLineWidth(context, 1);    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);    CGContextMoveToPoint(context, 0, 0);    CGContextAddLineToPoint(context, 150, 0);    CGContextStrokePath(context);            CGContextMoveToPoint(context, 150, 0);    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);    CGContextAddLineToPoint(context, 140, 5);    CGContextMoveToPoint(context, 150, 0);    CGContextAddLineToPoint(context, 140, -5);    CGContextStrokePath(context);        //画y    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);    CGContextMoveToPoint(context, 0, 0);    CGContextAddLineToPoint(context, 0, 150);    CGContextAddLineToPoint(context, 5, 140);    CGContextMoveToPoint(context, -5, 150);    CGContextAddLineToPoint(context, -5, 140);    CGContextStrokePath(context);    }

 

转载于:https://www.cnblogs.com/OIMM/p/5933806.html

你可能感兴趣的文章
多线程问题(JVM重排序)
查看>>
LeetCode 459 Repeated Substring Pattern
查看>>
POJ 3268 Silver Cow Party
查看>>
进程线程及堆栈关系的总结
查看>>
Android Camera开发:使用TextureView和SurfaceTexture预览Camera 基础拍照demo
查看>>
EMLS项目推进思考
查看>>
python_os 的知识点
查看>>
Eclipse快捷键 10个最有用的快捷键
查看>>
2018-2019-1 20165302 实验五 通讯协议设计
查看>>
git 提交跳过认证
查看>>
Reading papers_11(读Integrating local action elements for action analysis相关文章)
查看>>
快速寻找满足条件的两个数
查看>>
系统时钟初始化实例
查看>>
Linux磁盘分区,挂载
查看>>
grep命令的使用
查看>>
Titanium -- 运行app出现错误: TypeError: argument of type 'NoneType' is not iterable
查看>>
软件功能描述
查看>>
ES6转换为ES5
查看>>
京东校招java笔试题_试卷: 京东2019校招笔试Java开发工程师笔试题(1-)
查看>>
java怎么删除创建的类_一个java创建,删除,构建Jenkins等功能的JenkinsUtil工具类...
查看>>