美文网首页
雷达扫描的简单实现

雷达扫描的简单实现

作者: yidulishuang | 来源:发表于2016-08-25 13:03 被阅读0次

采用UIBezierPath和CoreGraphics绘制线圈和扫描器,并结合CABasicAnimation简单实现了雷达扫描界面,效果图如下:

Radar.gif

上代码:

这里要重写- (void)drawRect:(CGRect)rect方法

- (void)drawRect:(CGRect)rect {
//绘制线圈
    for (int i = 0; i < 4; i++) {
        CGFloat radius = self.bounds.size.width / 6.0 * i;
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0) radius:radius startAngle:i/4.0*2*M_PI endAngle:2*M_PI+i/4.0*2*M_PI clockwise:YES];
        //添加线圈绘制动画
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        animation.fromValue = @(0.f);
        animation.toValue = @(1.f);
        animation.repeatCount = INT_MAX;
        animation.duration = 2.f;
        
        CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
        shapeLayer.strokeColor = [UIColor colorWithRed:0 green:0 blue:0.7 alpha:1].CGColor;
        shapeLayer.fillColor = [UIColor clearColor].CGColor;
        shapeLayer.lineWidth = 1;
        shapeLayer.lineJoin = kCALineJoinRound;
        shapeLayer.lineCap = kCALineCapRound;
        shapeLayer.path = path.CGPath;
        [shapeLayer addAnimation:animation forKey:@"circleLayerAnimation"];
        
        [self.layer addSublayer:shapeLayer];

    }
    
//绘制雷达扫描器
    /*
    //第一种方法:UIBezierPath循环画扇形,给颜色不同的透明度
    for (int i = 0; i < 90; i++) {
        CGFloat alpha = i * 1.0 / 90;
        
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0)];
        [path addArcWithCenter:CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0) radius:self.bounds.size.width / 2.0 startAngle:i * M_PI / 180.f endAngle:(i + 1) * M_PI / 180.f clockwise:YES];
        [path closePath];
        

        CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
        shapeLayer.strokeColor = [UIColor redColor].CGColor;
        shapeLayer.fillColor = [UIColor colorWithRed:0 green:0.6 blue:0 alpha:alpha].CGColor;
        shapeLayer.lineWidth = 0;
        shapeLayer.lineJoin = kCALineJoinRound;
        shapeLayer.lineCap = kCALineCapRound;
        shapeLayer.path = path.CGPath;

        [self.layer addSublayer:shapeLayer];
        
    }
    */
    //第二种方法:CoreGraphics颜色渐变
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    NSArray* gradientColors = [NSArray arrayWithObjects:
                               (id)[UIColor whiteColor].CGColor,
                               (id)[UIColor purpleColor].CGColor, nil];
    CGFloat gradientLocations[] = {0, 1};
    
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace,
                                                        (__bridge CFArrayRef)gradientColors,
                                                        gradientLocations);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextMoveToPoint(context, self.bounds.size.width / 2.0, self.bounds.size.height/2.0);
    CGContextAddArc(context, self.bounds.size.width / 2.0, self.bounds.size.height/2.0, self.bounds.size.width / 2.0, 0 , 1.0/2 *M_PI, 0);
    CGContextClosePath(context);
    CGContextClip(context);
    
    
    CGPoint endshine;
    CGPoint startshine;
    startshine = CGPointMake(self.bounds.size.width, self.bounds.size.width / 2.0);
    endshine = CGPointMake(self.bounds.size.width / 2.0 ,self.bounds.size.height);
    CGContextDrawLinearGradient(context,gradient , startshine, endshine, kCGGradientDrawsAfterEndLocation);
    CGContextRestoreGState(context);
    
    //添加雷达扫描动画
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 3;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = INT_MAX;
    
    [self.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    
}

你可以在 这里下载到本文的代码

相关文章

  • 雷达扫描的简单实现

    采用UIBezierPath和CoreGraphics绘制线圈和扫描器,并结合CABasicAnimation简单...

  • SuperMap iClient3D for WebGL扩展开发

    最近有同事反馈有模拟雷达扫描效果的需求,需要表达雷达扫描的半径、扫描范围以及扫描过程动画,下面将通过使用Cesiu...

  • 基于SurfaceView实现雷达扫描

    先上效果图(没有录成gif格式~其实是旋转的很漂亮。) 雷达效果原先是自定义View来实现的,但是由于要不停的刷新...

  • 雷达扫描搜索视图实现

    最近项目需求,有个蓝牙搜索扫描的功能,美工给予了一张静态的搜索视图,而需要的效果是实现模仿雷达转动扫描的效果,下面...

  • canvas实现雷达扫描效果

    效果图: 代码如下: 好记性不如烂笔头,总结和记录工作学习中的点点滴滴,如有不对之处还请指教。 参考[1] js实...

  • 如何将2D激光雷达实现3D应用?

    2D激光雷达除了实现定位、建图、多点触摸等应用外,还能用来做3D建模与环境扫描。肯定会有人好奇,2D激光雷达扫描出...

  • iOS 雷达扫描效果

    最近闲的时候实现了一个雷达扫描的效果效果如下图 前言 swift 3.0 实现 了解 Layer anchorPo...

  • 高级UI<第二十一篇>:使用SweepGradient实现雷达扫

    使用梯度渲染/渐变渲染(SweepGradient)可以实现雷达扫描效果,它的初始角度是0,使用矩阵变换的旋转操作...

  • UGUI雷达图《一》

    今天我们使用UGUI来实现这样一个雷达图雷达.gif 简单说下思路主要是通过自己定义顶点的位置来实现绘制多边形。那...

  • Android下实现雷达扫描效果(学习)

    Android UI 前几天看见一篇文章Android 雷达扫描动画效果实现 就学习了一下,在此做了笔记并做了一些...

网友评论

      本文标题:雷达扫描的简单实现

      本文链接:https://www.haomeiwen.com/subject/wunosttx.html