swift版雷达扫描

跟大家分享一个雷达扫描得小东西,

制作这个效果分为以下几步:

1:首先画出范围


详见主要代码:

let context = UIGraphicsGetCurrentContext()

context?.move(to: CGPoint.init(x: 0, y: self.bounds.size.height*0.5))

context?.setStrokeColor(red: 8.0/255.0, green: 242.0/255.0, blue: 46.0/255.0, alpha: 1)

context?.setLineWidth(1)

context?.addLine(to: CGPoint.init(x: self.bounds.size.width, y: self.bounds.size.height*0.5))

context?.move(to: CGPoint.init(x: self.bounds.size.width*0.5, y: (self.bounds.size.height - self.bounds.size.width)*0.5))

context?.addLine(to:  CGPoint.init(x: self.bounds.size.width*0.5, y:self.bounds.size.height - (self.bounds.size.height - self.bounds.size.width)*0.5))

context?.closePath()

context?.drawPath(using: .stroke)

var sectionRadius = radius / CGFloat(sectionsNum)//这个是几个圈

for i in 0..<sectionsNum{

context?.setLineWidth(1)

context?.addArc(center: CGPoint.init(x: self.center.x, y: self.center.y), radius: CGFloat(sectionRadius - CGFloat(5*(sectionsNum - i - 1))), startAngle: 0, endAngle: CGFloat(2*M_PI), clockwise:false)

context?.drawPath(using: .stroke)

sectionRadius += radius / CGFloat(sectionsNum)

}


2:对于点的处理(主要是点在视图位置的处理,对于点这个类加一些动画操作,这里就不过多的说了),具体见主要代码详情

_ =  self.pointsView?.subviews.map({ $0.removeFromSuperview() })

let pointsNum = self.dataSource?.numberOfPointsInRadarView(radarView: self)

for index in 0..<Int(pointsNum!) {

let point:CGPoint = (self.dataSource?.radarView(radarView: self, positionForIndex: index))!

let posDirection = point.x

let posDistance = point.y

let pointView = WJFRadrarPointView.init(frame: CGRect.zero)

let customView = self.dataSource?.radarView(radarView: self, viewForIndex: index)

pointView.addSubview(customView!)

pointView.tag = index

pointView.frame = (customView?.frame)!

pointView.center = CGPoint.init(x: self.center.x+posDistance*sin(posDirection * CGFloat( M_PI / 180)), y: self.center.y + posDistance * cos(posDirection * CGFloat(M_PI / 180)))

pointView.delegate = self

pointView.alpha = 1.0

let fromTransform = pointsView?.transform.scaledBy(x: 0.1, y: 0.2)

pointView.transform = fromTransform!

let toTransform = pointView.transform.concatenating((pointView.transform.inverted()))

let delayInSeconds = 0.1 * Double(index)

//            DispatchQueue.aft

self.pointsView?.addSubview(pointView)

pointView.transform = toTransform

}


3:就是前面GIF展示的,对于扫描区的处理

3.1创建扫描区,主要就是 创建一个三角弧区域

override func draw(_ rect: CGRect) {

super.draw(rect)

//画布

let context = UIGraphicsGetCurrentContext()

let startColor = self.startColor

context?.setFillColor((startColor?.cgColor)!)//填充颜色

context?.setLineWidth(0)

context?.move(to: CGPoint.init(x: self.center.x, y: self.center.y))

context?.addArc(center: CGPoint.init(x: self.center.x, y: self.center.y) , radius: self.radius!, startAngle: ((self.clockwise! ? self.anglee! : 0) * CGFloat(M_PI / 180)), endAngle: (self.clockwise! ? (self.anglee! - 1) : 1) * CGFloat(M_PI / 180), clockwise: self.clockwise!)

context?.closePath()

context?.drawPath(using: .fillStroke)

let startColorComponents = (self.startColor?.cgColor)?.components

let endColorComponents = (self.endColor?.cgColor)?.components

var R:CGFloat = 0.0

var G:CGFloat = 0.0

var B:CGFloat = 0.0

var A:CGFloat = 0.0

for index in 0...Int(self.anglee!) {

let ratio = (self.clockwise! ? (self.anglee! - CGFloat(index)) : CGFloat(index)) / self.anglee!

R = (startColorComponents?[0])! - ((startColorComponents?[0])! - (endColorComponents?[0])!) * ratio

G = (startColorComponents?[1])! - ((startColorComponents?[1])! - (endColorComponents?[1])!) * ratio

B = (startColorComponents?[2])! - ((startColorComponents?[2])! - (endColorComponents?[2])!) * ratio

A = (startColorComponents?[3])! - ((startColorComponents?[3])! - (endColorComponents?[3])!) * ratio

let startColor = UIColor.init(red: R, green: G, blue: B, alpha: A)

context?.setFillColor(startColor.cgColor)

context?.setLineWidth(0)

context?.move(to: CGPoint.init(x: self.center.x, y: self.center.y))

context?.addArc(center: CGPoint.init(x: self.center.x, y: self.center.y) , radius: self.radius!, startAngle: CGFloat(Double(index) * M_PI / 180), endAngle: CGFloat((Double(index) + (self.clockwise! ? -1 : 1)) * M_PI / 180), clockwise: self.clockwise!)

context?.closePath()

context?.drawPath(using: .fillStroke)

}}

3.2 让这个区域动起来,比较简单的一个核心动画

let rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z")

var indicatorClockwise = true

if self.indicatorClockwise != nil {

indicatorClockwise = self.indicatorClockwise!

}

rotationAnimation.toValue = (indicatorClockwise ? 1 : -1) * M_PI * 2.0

rotationAnimation.duration = 360.0 / 60

rotationAnimation.isCumulative = true

rotationAnimation.repeatCount = Float(INT_MAX)

self.indicatorView?.layer.add(rotationAnimation, forKey: "rotationAnimation")

至此,swift版的雷达简单版就出来了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,636评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,890评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,680评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,766评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,665评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,045评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,515评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,182评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,334评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,274评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,319评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,002评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,599评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,675评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,917评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,309评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,885评论 2 341

推荐阅读更多精彩内容

  • #define kBlackColor [UIColor blackColor] //.h //划线 + (voi...
    CHADHEA阅读 775评论 0 1
  • Quartz2D以及drawRect的重绘机制字数1487 阅读21 评论1 喜欢1一、什么是Quartz2D Q...
    PurpleWind阅读 757评论 0 3
  • SwiftDay011.MySwiftimport UIKitprintln("Hello Swift!")var...
    smile丽语阅读 3,824评论 0 6
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,083评论 5 13
  • VLC的集成和使用 VLC介绍 VLC Media Player (VideoLAN) 为 Windows、Lin...
    Pocket阅读 19,421评论 75 66