广告
返回顶部
首页 > 资讯 > 移动开发 >iOS实现雷达扫描效果
  • 412
分享到

iOS实现雷达扫描效果

iOS雷达扫描 2022-05-27 09:05:25 412人浏览 泡泡鱼
摘要

本文实例为大家分享了iOS实现雷达扫描的具体代码,供大家参考,具体内容如下 #import <UIKit/UIKit.h> @interface LTIndic

本文实例为大家分享了iOS实现雷达扫描的具体代码,供大家参考,具体内容如下


#import <UIKit/UIKit.h>
 
@interface LTIndicatiorView : UIView
@property(nonatomic,strong)UIColor *color;
@property(nonatomic,assign)float repeatCount;
@property(nonatomic,strong)UIColor *borderColor;
@property(nonatomic,assign)float borderWidth;
@end
 
@interface LTRadarView : UIView
@property(nonatomic,strong)UIColor *color;
@property(nonatomic,strong)UIColor *borderColor;
@property(nonatomic,assign)float borderWidth;
@property(nonatomic,assign)int pulsinGCount;
@property(nonatomic,assign)float duration;
@property(nonatomic,assign)float repeatCount;
@property(nonatomic,strong)CALayer *pulsingLayer;
 
 
@end

.m文件


//
//  LTRadarView.m
//  raderScan
//
//  Created by Mac on 17/2/5.
//  Copyright © 2017年 mac. All rights reserved.
//
 
#import "LTRadarView.h"
 
#define Angel 15
 
@interface LTRadarButton : UIButton
 
@end
 
@implementation LTRadarButton
 
- (void)removeFromSuperview
{
    [UIView beginAnimations:@"" context:nil];
    [UIView setAnimationDuration:0.5];
    self.transfORM = CGAffineTransformMakeScale(0.2, 0.2);
    self.alpha = 0;
    [UIView setAnimationDidStopSelector:@selector(callSuperRemoveFromSuperview)];
    [UIView commitAnimations];
 
}
 
- (void)callSuperRemoveFromSuperview
{
    [super removeFromSuperview];
}
 
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    self.alpha = 0;
    return self;
}
 
- (void)didMoveToWindow
{
    [super didMoveToWindow];
     self.transform = CGAffineTransformMakeScale(0.2, 0.2);
    if (self.window) {
        [UIView animateWithDuration:0.5 animations:^{
            self.transform = CGAffineTransformIdentity;
            self.alpha = 1;
        }];
    }
    
}
 
 
 
@end
 
 
@implementation LTIndicatiorView
 
- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _color = [UIColor greenColor];
        _repeatCount = HUGE_VALF;
        _borderColor = [UIColor redColor];
        _borderWidth = 1.0f;
    }
    return self;
}
 
 
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
     
     self.backgroundColor = [UIColor whiteColor];
     [super drawRect:rect];
     self.layer.cornerRadius = self.frame.size.height/2.0f;
     self.clipsToBounds = YES;
     self.layer.borderColor = [UIColor clearColor].CGColor;
     self.layer.borderWidth = 50;
     self.layer.masksToBounds = YES;
     
     
     CGContextRef context = UIGraphicsGetCurrentContext();
     for (int i = 0; i < Angel; i++) {
         CGFloat alpha = (float)i /(float)600;
         CGColorRef shadowColor = [[UIColor greenColor] colorWithAlphaComponent:alpha].CGColor;//计算扇形填充颜色
         CGContextSetFillColorWithColor(context, shadowColor);
         CGContextMoveToPoint(context, self.center.x, self.center.y);//指定员心
         CGFloat startAngle =  (-Angel+i+1.15)/Angel*(float)M_PI;
         CGFloat endAngle = (-Angel+i-1.15)/Angel*(float)M_PI;
//         NSLog(@"startAngle = %f endAngle = %f ,alpha = %f",startAngle,endAngle,alpha);
         CGContextAddArc(context, self.center.x, self.center.y, self.frame.size.height/2.0f,0, 25, 1);//画一个扇形
         CGContextClosePath(context);
         
         CGContextDrawPath(context, kCGPathFill);//绘制扇形
 
     }
     
     CGContextSetLineWidth(context, 1);//扫描线宽度
     CGContextSetStrokeColorWithColor(context, [_color colorWithAlphaComponent:1].CGColor);//扫描线颜色
     CGContextMoveToPoint(context, self.center.x, self.center.y);
     CGContextAddLineToPoint(context, self.frame.size.height, self.center.y);
     CGContextStrokePath(context);
     
     CGContextSetRGBStrokeColor(context,255/255.0, 255/255.0, 255/255.0, 0.1);//最外面圆颜色
     CGContextSetLineWidth(context, 10);//线宽度
     CGContextAddArc(context, self.center.x, self.center.y, self.frame.size.height/2.0, 0, 2*M_PI, 1);//添加一个圆
     CGContextDrawPath(context, kCGPathStroke);//绘制路径
     
     CGContextStrokePath(context);//显示绘制
     
     
     //扫描动画
     CABasicAnimation *rotateAnimation = [CABasicAnimation animation];
     rotateAnimation.keyPath = @"transform.rotation.z";
     rotateAnimation.toValue = @(2*M_PI);
     rotateAnimation.duration = 3;
     rotateAnimation.removedOnCompletion = NO;
     rotateAnimation.repeatCount = _repeatCount;
    
     [self.layer addAnimation:rotateAnimation forKey:@"rotate_layer"];
 }
 
 
@end
 
 
@implementation LTRadarView
{
    NSMutableArray *items;
}
 
 
- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        items = [NSMutableArray array];
        _color = [UIColor redColor];
      
        _borderColor = [UIColor greenColor];
        _pulsingCount = 3;
        _duration = 3;
        _repeatCount = HUGE_VALF;
        _borderWidth = 3.0f;
    }
    return self;
}
 
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [super drawRect:rect];
    
    self.layer.cornerRadius = self.frame.size.height/2;
    self.clipsToBounds = YES;
    self.layer.borderColor = [UIColor clearColor].CGColor;
    self.layer.borderColor = [UIColor clearColor].CGColor;
    self.layer.borderWidth = 50;
    self.layer.masksToBounds = YES;
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(ctx, 0/255.0, 0/255.0, 0/255.0, 1);//圆颜色
    CGContextSetLineWidth(ctx, 1);//宽度
    CGContextAddArc(ctx, self.center.x, self.center.y, self.frame.size.height/2, 0, 2*M_PI, 1);//添加一个圆
    CGContextDrawPath(ctx, kCGPathStroke);//绘制
    CGContextStrokePath(ctx);//显示
    
    CALayer *animationLayer = [CALayer layer];
    animationLayer.frame = self.layer.frame;
    for (int i = 0; i < _pulsingCount; i++) {
        
        CALayer *pulsingLayer = [CALayer layer];
        pulsingLayer.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
        pulsingLayer.borderColor = [UIColor clearColor].CGColor;
        pulsingLayer.borderWidth = 1;
        pulsingLayer.cornerRadius = rect.size.height/2;
        pulsingLayer.backgroundColor = [UIColor redColor].CGColor;
        
        CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
        animationGroup.fillMode = kCAFillModeBoth;
        animationGroup.beginTime = CACurrentMediaTime() + (float) i * _duration / _pulsingCount;
        animationGroup.duration = _duration;
        animationGroup.repeatCount = HUGE_VALF;
        animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        animationGroup.autoreverses = NO;
        animationGroup.delegate = self;
        animationGroup.removedOnCompletion = NO;
        
        CABasicAnimation *scaleAnimation = [CABasicAnimation animation];
        scaleAnimation.keyPath = @"transform.scale";
        scaleAnimation.removedOnCompletion = NO;
        scaleAnimation.fromValue = @(0.0f);
        scaleAnimation.toValue = @1.0f;
        scaleAnimation.autoreverses = NO;
        
        
        CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimation animation];
        opacityAnimation.keyPath = @"opacity";
        opacityAnimation.values = @[@1.0,@0.75,@0.5,@0.25,@0.0];
        opacityAnimation.keyTimes = @[@0.0,@0.25,@0.5,@0.75,@1];
        opacityAnimation.autoreverses = NO;
        opacityAnimation.removedOnCompletion = NO;
        
        animationGroup.animations = @[scaleAnimation,opacityAnimation];
        [pulsingLayer addAnimation:animationGroup forKey:@"pulsing"];
        
        [animationLayer addSublayer:pulsingLayer];
    }
    
    [self.layer addSublayer:animationLayer];
    
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(addOrReplaceItem) userInfo:nil repeats:YES];
}
 
 
- (void)animation:(CALayer *)layer
{
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.fillMode = kCAFillModeBoth;
    animationGroup.beginTime = CACurrentMediaTime() + 1 * _duration / _pulsingCount;
    animationGroup.duration = _duration;
    animationGroup.repeatCount = HUGE_VALF;
    animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animationGroup.autoreverses = NO;
    animationGroup.delegate = self;
    animationGroup.removedOnCompletion = NO;
    
    CABasicAnimation *scaleAnimation = [CABasicAnimation animation];
    scaleAnimation.keyPath = @"transform.scale";
    scaleAnimation.removedOnCompletion = NO;
    scaleAnimation.fromValue = @(0.0f);
    scaleAnimation.toValue = @1.0f;
    scaleAnimation.autoreverses = NO;
    
    
    CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimation animation];
    opacityAnimation.keyPath = @"opacity";
    opacityAnimation.values = @[@1.0,@0.75,@0.5,@0.25,@0.0];
    opacityAnimation.keyTimes = @[@0.0,@0.25,@0.5,@0.75,@1];
    opacityAnimation.autoreverses = NO;
    opacityAnimation.removedOnCompletion = NO;
    
    animationGroup.animations = @[scaleAnimation,opacityAnimation];
    [layer addAnimation:animationGroup forKey:@"pulsing"];
    
    
}
 
#define RandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]

 
- (CGPoint)generateCenterPointInRadar
{
    float  angle = arc4random() % 360;//随机一个角度
    float radius = arc4random() % (int)((self.bounds.size.width - 44)/2);//随机一个半径, 这里减去44是因为要把这个view显示在圆里面,如果不减44,则有可能会显示在圓外面
    double x = cos(angle) * radius;//计算随机出现的一个角度的x坐标 x=a+r*cosθ r = radius, θ = angle ,a = 圆心的x坐标
    double y = sin(angle) * radius;//计算随机出现的一个角度的y坐标 y=b+r*sinθ r = radius, θ = angle ,b = 圆心的y坐标
    return CGPointMake(x + self.bounds.size.width / 2, y + self.bounds.size.height / 2);//x y 分别加个圆心的坐标即self.center.x.y
}
 
- (void)addOrReplaceItem
{
    int maxCount = 10;
    
    LTRadarButton *radarButton = [LTRadarButton buttonWithType:UIButtonTypeCustom];
    radarButton.frame = CGRectMake(0, 0, 44, 44);
    radarButton.backgroundColor = RandomColor;
    radarButton.layer.cornerRadius = 44/2;
    
 
    do {
        CGPoint center = [self generateCenterPointInRadar];
        radarButton.center = CGPointMake(center.x, center.y);
    } while ([self itemFrameIntersectsInOtherItem:radarButton.frame]);
        
 
    [self addSubview:radarButton];
    [items addObject:radarButton];
    
    if (items.count > maxCount)
    {
        UIView * view = [items firstObject];
        [view removeFromSuperview];
        [items removeObject:view];
    }
}
 
 

- (BOOL)itemFrameIntersectsInOtherItem:(CGRect)frame
{
    for (UIView *item in items)
    {
        if (CGRectIntersectsRect(item.frame, frame))
        {
            return YES;
        }
    }
    return NO;
}

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

--结束END--

本文标题: iOS实现雷达扫描效果

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

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

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

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

下载Word文档
猜你喜欢
  • iOS实现雷达扫描效果
    本文实例为大家分享了iOS实现雷达扫描的具体代码,供大家参考,具体内容如下 #import <UIKit/UIKit.h> @interface LTIndic...
    99+
    2022-05-27
    iOS 雷达扫描
  • Android编程简单实现雷达扫描效果
    本文实例讲述了Android编程简单实现雷达扫描效果。分享给大家供大家参考,具体如下: 在eoe看到有一篇关于雷达扫描的文章,然后看了下,很简单,但是觉得还有很多可以优化的地方...
    99+
    2022-06-06
    雷达 Android
  • Android动画之雷达扫描效果
    我们首先看一下效果图,有个整体的印象 好了,为了便于理解,这里就按照动画所见内容依次展开来说 准备 这里决定采用canvas(画布)和paint(画笔)实现了这个简单动画控...
    99+
    2022-06-06
    雷达 Android
  • 利用Qt绘制雷达扫描效果
    话不多说直接上代码,代码规范性可能差了点,但是效果得以实现,在这里记录一下。 scan.h的代码如下 #ifndef SCAN_H #define SCAN_H #include ...
    99+
    2023-05-14
    Qt实现雷达扫描效果 Qt雷达扫描 Qt雷达
  • Android仿微信雷达扫描效果的实现方法
    本文主要给大家介绍的是关于Android实现微信雷达扫描效果的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:废话不多说 先上图(用AS录制的 转换工具不是很好 所以看得效果不是很好)效果图示例代码Activity 代码public...
    99+
    2023-05-31
    android 雷达扫描
  • 怎么用Qt绘制雷达扫描效果
    本文小编为大家详细介绍“怎么用Qt绘制雷达扫描效果”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么用Qt绘制雷达扫描效果”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。scan.h的代码如下#ifndef&nb...
    99+
    2023-07-05
  • Android Shader应用开发之雷达扫描效果
    本文实例为大家分享了Android雷达扫描效果的具体代码,供大家参考,具体内容如下效果图知识点提要 Shader 矩阵matrix 属性动画 ShaderView3package com.example.apple.shaderde...
    99+
    2023-05-30
    android 雷达扫描 roi
  • iOS自定义雷达扫描扩散动画
    本文实例为大家分享了iOS实现雷达扫描扩散动画的具体代码,供大家参考,具体内容如下 自己自定义了 一个雷达扫描/扩散效果的View。 扫描View 效果如下: 扩散View 效果如...
    99+
    2022-05-27
    iOS 扫描 扩散
  • CSS3如何实现雷达扫描图
    这篇文章给大家分享的是有关CSS3如何实现雷达扫描图的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。通过css3实现炫酷的雷达扫描图:直接上代码:// index.html<!DOCTYPE&nbs...
    99+
    2023-06-08
  • Android仿微信、QQ附近好友雷达扫描效果
    1.概述   最近一直到在带实习生,因为人比较多,所以很长一段时间没有更新博客了,今天更新一篇雷达扫描附近好友效果,以后尽量每周更新一篇,先看一下效果: 2.实现  1、...
    99+
    2022-06-06
    雷达 Android
  • WPF实现雷达扫描图的绘制详解
    目录前言制作思路具体实现前言 实现一个雷达扫描图。 源代码在TK_King/雷达 (gitee.com),自行下载就好了 制作思路 绘制圆形(或者称之轮)绘制分割线绘制扫描范围添加...
    99+
    2022-11-13
  • WPF怎么实现雷达扫描图的绘制
    这篇文章主要介绍了WPF怎么实现雷达扫描图的绘制的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇WPF怎么实现雷达扫描图的绘制文章都会有所收获,下面我们一起来看看吧。制作思路绘制圆形(或者称之轮)绘制分割线绘制扫...
    99+
    2023-06-30
  • Flutter实现旋转扫描效果
    效果图: 1 .测试Demo启动文件 main() { runApp(MaterialApp( home: SignSwiperPage(), )); } cla...
    99+
    2022-11-13
  • Android自定义View实现扫描效果
    本文实例为大家分享了Android自定义View实现扫描效果的具体代码,供大家参考,具体内容如下 演示效果如下: 实现内容: 1、控制动画是竖向或者横向 2、控制动画初始是从底部/...
    99+
    2022-11-12
  • python实现打印扫描效果详情
    目录1. 介绍2. 完整代码 1. 介绍 前面我们尝试通过python实现了代码雨以及字母随机闪烁的效果,这次,我们再来实现一个代码的线性扫面。 同样的,此次我们仍然是使用30行代码...
    99+
    2022-11-11
  • CSS3如何实现雷达圈动画效果
    这篇文章主要介绍了CSS3如何实现雷达圈动画效果的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇CSS3如何实现雷达圈动画效果文章都会有所收获,下面我们一起来看看吧。效果:html代码:<body ...
    99+
    2023-07-04
  • Android自定义控件实现雷达图效果
    本文实例为大家分享了Android自定义控件实现雷达图的具体代码,供大家参考,具体内容如下 学习了大神的源代码(奈何不知大神的博客地址),觉得必须记录一下,方便以后再次学习。 效果如...
    99+
    2022-11-13
  • iOS 二维码扫描相关功能实现
    写在前面 最近项目要实现相机扫描二维码功能,具体要求:1、扫描框 2、扫描动画 3、相册识别二维码 4、声音反馈。 记得之前用过三方库做过类似功能,但是也是知其然不知其所以然,然后今...
    99+
    2022-06-04
    iOS 二维码 扫描
  • 基于Android自定义控件实现雷达效果
    如何制作出类似雷达扫描的效果,具体方法如下一、效果图二、实现思路 自定义控件RadarView用来画雷达的效果图,可以自定义属性包括 backgroundColor:背景颜色 circleNum:圆的数量 startColor:开始颜色 e...
    99+
    2023-05-30
    android 雷达 roi
  • Android基于google Zxing实现各类二维码扫描效果
    随着微信的到来,二维码越来越火爆,随处能看到二维码,比如商城里面,肯德基,餐厅等等,对于二维码扫描我们使用的是google的开源框架Zxing,我们可以去http://code...
    99+
    2022-06-06
    二维码扫描 google zxing 二维 二维码 Android
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作