Created
February 14, 2018 05:38
-
-
Save JohnEstropia/0926c8ee64744f29465ceffa27e22ad9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RadialView.swift | |
// | |
// | |
// Created by John Rommel Estropia on 2016/08/06. | |
// Copyright © 2016 JohnEstropia. All rights reserved. | |
// | |
import UIKit | |
// MARK: - RadialView | |
class RadialView: UIView { | |
var innerColor = UIColor.clear { | |
didSet { | |
self.setNeedsDisplay() | |
} | |
} | |
var outerColor = UIColor.clear { | |
didSet { | |
self.setNeedsDisplay() | |
} | |
} | |
var innerOffset: CGFloat = 0 { | |
didSet { | |
self.setNeedsDisplay() | |
} | |
} | |
var outerOffset: CGFloat = 1 { | |
didSet { | |
self.setNeedsDisplay() | |
} | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
self.setup() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
self.setup() | |
} | |
// MARK: NSObject | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
self.setup() | |
} | |
// MARK: UIView | |
override func draw(_ rect: CGRect) { | |
let startCenter = CGPoint(x: rect.midX, y: rect.midY) | |
let endCenter = startCenter | |
let startRadius: CGFloat = 0 | |
let endRadius = hypot(startCenter.x, startCenter.y) | |
let locations = [self.innerOffset, self.outerOffset] | |
var components = Array<CGFloat>(repeating: 0, count: 8); | |
do { | |
let innerColor = self.innerColor.cgColor | |
if innerColor.numberOfComponents == 4 { | |
let partialComponents = innerColor.components! | |
components[0] = partialComponents[0] | |
components[1] = partialComponents[1] | |
components[2] = partialComponents[2] | |
components[3] = partialComponents[3] | |
} | |
else { | |
let partialComponents = innerColor.components! | |
components[0] = partialComponents[0] | |
components[1] = partialComponents[0] | |
components[2] = partialComponents[0] | |
components[3] = partialComponents[1] | |
} | |
} | |
do { | |
let outerColor = self.outerColor.cgColor | |
if outerColor.numberOfComponents == 4 { | |
let partialComponents = outerColor.components! | |
components[4] = partialComponents[0] | |
components[5] = partialComponents[1] | |
components[6] = partialComponents[2] | |
components[7] = partialComponents[3] | |
} | |
else { | |
let partialComponents = outerColor.components! | |
components[4] = partialComponents[0] | |
components[5] = partialComponents[0] | |
components[6] = partialComponents[0] | |
components[7] = partialComponents[1] | |
} | |
} | |
UIGraphicsGetCurrentContext()?.drawRadialGradient(CGGradient(colorSpace: CGColorSpaceCreateDeviceRGB(), colorComponents: components, locations: locations, count: 2)!, | |
startCenter: startCenter, | |
startRadius: startRadius, | |
endCenter: endCenter, | |
endRadius: endRadius, | |
options: .drawsAfterEndLocation | |
) | |
} | |
// MARK: Private | |
private func setup() { | |
self.contentMode = .redraw | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment