2015-12-31 15:41:32 -08:00
|
|
|
//
|
|
|
|
|
// SwitchIndicatorView.swift
|
|
|
|
|
// XIONControlPanel
|
|
|
|
|
//
|
|
|
|
|
// Created by Charles Magahern on 12/31/15.
|
|
|
|
|
// Copyright © 2015 XION. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
2016-07-24 00:14:04 -07:00
|
|
|
class SwitchIndicatorView: UIView
|
|
|
|
|
{
|
2017-05-13 00:40:03 -07:00
|
|
|
fileprivate var _status: Bool = false
|
|
|
|
|
fileprivate var _foregroundColor: UIColor = UIColor.white
|
|
|
|
|
fileprivate var _outerCircleLayer: CAShapeLayer = CAShapeLayer()
|
|
|
|
|
fileprivate var _offSymbolLayer: CAShapeLayer = CAShapeLayer()
|
|
|
|
|
fileprivate var _onSymbolLayer: CAShapeLayer = CAShapeLayer()
|
2015-12-31 15:41:32 -08:00
|
|
|
|
2017-05-13 00:40:03 -07:00
|
|
|
static fileprivate var lineWidth: CGFloat = 2.0
|
2015-12-31 15:41:32 -08:00
|
|
|
|
|
|
|
|
override init(frame: CGRect)
|
|
|
|
|
{
|
|
|
|
|
super.init(frame: frame)
|
|
|
|
|
|
2017-05-13 00:40:03 -07:00
|
|
|
_outerCircleLayer.fillColor = UIColor.clear.cgColor
|
2015-12-31 15:41:32 -08:00
|
|
|
_outerCircleLayer.lineWidth = SwitchIndicatorView.lineWidth
|
|
|
|
|
|
2017-05-13 00:40:03 -07:00
|
|
|
_offSymbolLayer.fillColor = UIColor.clear.cgColor
|
2015-12-31 15:41:32 -08:00
|
|
|
_offSymbolLayer.lineWidth = SwitchIndicatorView.lineWidth
|
|
|
|
|
|
|
|
|
|
self.layer.addSublayer(_outerCircleLayer)
|
|
|
|
|
self.layer.addSublayer(_offSymbolLayer)
|
|
|
|
|
self.layer.addSublayer(_onSymbolLayer)
|
|
|
|
|
|
|
|
|
|
self.status = false
|
2017-05-13 00:40:03 -07:00
|
|
|
self.foregroundColor = UIColor.white
|
2015-12-31 15:41:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder)
|
|
|
|
|
{
|
|
|
|
|
fatalError("unsupported")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func layoutSubviews()
|
|
|
|
|
{
|
|
|
|
|
super.layoutSubviews()
|
|
|
|
|
|
|
|
|
|
let bounds = self.bounds
|
|
|
|
|
let lineWidth = SwitchIndicatorView.lineWidth
|
|
|
|
|
|
|
|
|
|
_outerCircleLayer.frame = bounds
|
|
|
|
|
_offSymbolLayer.frame = bounds
|
|
|
|
|
_onSymbolLayer.frame = bounds
|
|
|
|
|
|
2017-05-13 00:40:03 -07:00
|
|
|
_outerCircleLayer.path = CGPath(ellipseIn: bounds, transform: nil)
|
2015-12-31 15:41:32 -08:00
|
|
|
|
|
|
|
|
let innerSize = CGSize(width: rint(bounds.size.width / 2.0), height: rint(bounds.size.height / 2.0))
|
|
|
|
|
let innerBounds = CGRect(
|
|
|
|
|
x: rint(bounds.size.width / 2.0 - innerSize.width / 2.0),
|
|
|
|
|
y: rint(bounds.size.height / 2.0 - innerSize.height / 2.0),
|
|
|
|
|
width: innerSize.width,
|
|
|
|
|
height: innerSize.height
|
|
|
|
|
)
|
2017-05-13 00:40:03 -07:00
|
|
|
_offSymbolLayer.path = CGPath(ellipseIn: innerBounds, transform: nil)
|
2015-12-31 15:41:32 -08:00
|
|
|
|
|
|
|
|
let onSymbolSize = CGSize(width: lineWidth, height: innerSize.height)
|
|
|
|
|
let onSymbolRect = CGRect(
|
|
|
|
|
x: rint(bounds.size.width / 2.0 - onSymbolSize.width / 2.0),
|
|
|
|
|
y: rint(bounds.size.height / 2.0 - onSymbolSize.height / 2.0),
|
|
|
|
|
width: onSymbolSize.width,
|
|
|
|
|
height: onSymbolSize.height
|
|
|
|
|
)
|
2017-05-13 00:40:03 -07:00
|
|
|
_onSymbolLayer.path = CGPath(rect: onSymbolRect, transform: nil)
|
2015-12-31 15:41:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: API
|
|
|
|
|
|
2015-12-31 17:30:23 -08:00
|
|
|
var status: Bool
|
|
|
|
|
{
|
2015-12-31 15:41:32 -08:00
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _status
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set(status)
|
|
|
|
|
{
|
|
|
|
|
_status = status
|
2017-05-13 00:40:03 -07:00
|
|
|
_offSymbolLayer.isHidden = _status
|
|
|
|
|
_onSymbolLayer.isHidden = !_status
|
2015-12-31 15:41:32 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 17:30:23 -08:00
|
|
|
var foregroundColor: UIColor
|
|
|
|
|
{
|
2015-12-31 15:41:32 -08:00
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _foregroundColor
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set(color)
|
|
|
|
|
{
|
|
|
|
|
_foregroundColor = color
|
2017-05-13 00:40:03 -07:00
|
|
|
_outerCircleLayer.strokeColor = _foregroundColor.cgColor
|
|
|
|
|
_offSymbolLayer.strokeColor = _foregroundColor.cgColor
|
|
|
|
|
_onSymbolLayer.fillColor = _foregroundColor.cgColor
|
2015-12-31 15:41:32 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|