首页 > 代码库 > 雷达扫描动画

雷达扫描动画

 

 

 

#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 <UIKit/UIKit.h>#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 <UIKit/UIKit.h>@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 = http://www.mamicode.com/@(2*M_PI);"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);    }

 

雷达扫描动画