Skip to content

Instantly share code, notes, and snippets.

@nmondollot
Created December 16, 2020 21:14
Show Gist options
  • Save nmondollot/6cda942b9c4f130b91a27fac7ee7d9f9 to your computer and use it in GitHub Desktop.
Save nmondollot/6cda942b9c4f130b91a27fac7ee7d9f9 to your computer and use it in GitHub Desktop.
iOS-14-style heading subview for MKUserLocation
//
// HeadingView.swift
//
// Created by Nicolas Mondollot on 10/12/2020.
// License: Attribution 4.0 International (CC BY 4.0)
//
import UIKit
import CoreLocation
class HeadingView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
didLoad()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
didLoad()
}
var headingAccuracy: CLLocationDirectionAccuracy = 90 {
didSet {
headingArcLayer.path = arcPath(angleInDegrees: CGFloat(headingAccuracy))
headingArcLayer.isHidden = (headingAccuracy >= 90)
}
}
private var headingArcLayer: CAShapeLayer!
private func didLoad() {
if headingArcLayer == nil {
headingArcLayer = CAShapeLayer()
headingArcLayer.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)
let gradientLayer = CAGradientLayer()
gradientLayer.type = .radial
gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)
gradientLayer.colors = [
UIColor.systemBlue.withAlphaComponent(0.0).cgColor,
UIColor.systemBlue.withAlphaComponent(0.8).cgColor,
UIColor.systemBlue.withAlphaComponent(0.0).cgColor
]
gradientLayer.frame = bounds
gradientLayer.mask = headingArcLayer
layer.addSublayer(gradientLayer)
}
}
private func arcPath(angleInDegrees: CGFloat) -> CGPath {
let bezierPath = UIBezierPath(
arcCenter: CGPoint(x: bounds.size.width/2, y: bounds.size.height/2),
radius: bounds.size.height/2,
startAngle: (-90-angleInDegrees) * CGFloat.pi / 180,
endAngle: (-90+angleInDegrees) * CGFloat.pi / 180,
clockwise: true
)
bezierPath.addLine(to: CGPoint(x: bounds.size.width/2, y: bounds.size.height/2))
return bezierPath.cgPath
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment