Skip to content

Commit

Permalink
애니메이션 적용 완
Browse files Browse the repository at this point in the history
  • Loading branch information
youkchansim committed Mar 6, 2017
1 parent f5f47e0 commit 11dc208
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 148 deletions.
152 changes: 93 additions & 59 deletions CSPieChart/Classes/CSPieChart.swift
Expand Up @@ -31,20 +31,58 @@ public class CSPieChart: UIView {

fileprivate var selectedComponent: CSPieChartComponent?

override public func draw(_ rect: CGRect) {
super.draw(rect)
fileprivate var componentList: [CSPieChartComponent] = []

fileprivate var container = CALayer()
fileprivate var animated = false

override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let location = touches.first?.location(in: self) else { return }
guard let layers = layer.sublayers else { return }
guard let component = (layers.flatMap { $0 as? CSPieChartComponent }.filter { $0.containPoint(point: location) }.first) else { return }

if !component.isAnimated {
component.startAnimation(animationType: seletingAnimationType)
selectedComponent = component
} else {
component.stopAnimation(animationType: seletingAnimationType)
selectedComponent = nil
}
}

override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let location = touches.first?.location(in: self) else { return }
guard let component = selectedComponent else { return }

if !component.containPoint(point: location) {
component.stopAnimation(animationType: seletingAnimationType)
selectedComponent = nil
}
}

override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let location = touches.first?.location(in: self) else { return }
guard let component = selectedComponent else { return }

if component.containPoint(point: location) {
delegate?.didSelectedPieChartComponent?(at: component.index!)
}

component.stopAnimation(animationType: seletingAnimationType)
selectedComponent = nil
}
}

public extension CSPieChart {
public func show(animated: Bool) {
clear()

self.animated = animated

if let itemCount = dataSource?.numberOfComponentData() {

var sum: Double = 0
for index in 0..<itemCount {
if let data = dataSource?.pieChartComponentData(at: index) {
sum += data.value
}
}
let sum = getTotalValueSum(count: itemCount)

var startAngle: CGFloat = 0

if itemCount > 0 {
if let data = dataSource?.pieChartComponentData(at: 0) {
startAngle = (sum / data.value).toRadian
Expand All @@ -56,80 +94,76 @@ public class CSPieChart: UIView {
let degree: Double = data.value / sum * 360
let endAngle = startAngle + degree.toRadian

var componentColor: UIColor = .white
if let componentColorsCount = dataSource?.numberOfComponentColors() {
let compoenetColorIndex = index % componentColorsCount
componentColor = dataSource?.pieChartComponentColor(at: compoenetColorIndex) ?? .white
}

var lineColor: UIColor = .black
if let lineColorsCount = dataSource?.numberOfLineColors?() {
let lineColorIndex = index % lineColorsCount
lineColor = dataSource?.pieChartLineColor?(at: lineColorIndex) ?? .black
}

var subView: UIView?
if let subViewsCount = dataSource?.numberOfComponentSubViews?() {
let subViewIndex = index % subViewsCount
subView = dataSource?.pieChartComponentSubView?(at: subViewIndex) ?? UIView()
}
let componentColor = getComponentColor(index: index)
let lineColor = getLineColor(index: index)
let subView = getSubView(index: index)

let component = CSPieChartComponent(frame: bounds, startAngle: startAngle, endAngle: endAngle, data: data, index: index, radiusRate: pieChartRadiusRate)
component.componentColor = componentColor
component.subView = subView?.layer
component.lineColor = lineColor
component.lineLength = pieChartLineLength
component.animate(animated: animated)

layer.addSublayer(component)
component.setNeedsDisplay()
componentList.append(component)

startAngle = endAngle
}
}
}
}

override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let location = touches.first?.location(in: self) else { return }
guard let layers = layer.sublayers else { return }
guard let component = (layers.flatMap { $0 as? CSPieChartComponent }.filter { $0.path?.contains(location) ?? false }.first) else { return }

if !component.isAnimated {
component.startAnimation(animationType: seletingAnimationType)
selectedComponent = component
} else {
component.stopAnimation(animationType: seletingAnimationType)
selectedComponent = nil
public func reloadPieChart() {
clear()
show(animated: animated)
}
}

fileprivate extension CSPieChart {
func getTotalValueSum(count: Int) -> Double {
var sum: Double = 0
for index in 0..<count {
if let data = dataSource?.pieChartComponentData(at: index) {
sum += data.value
}
}

return sum
}

override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let component = selectedComponent else { return }
guard let location = touches.first?.location(in: self) else { return }

if !component.containsPoint(point: location) {
component.stopAnimation(animationType: seletingAnimationType)
selectedComponent = nil
func getComponentColor(index: Int) -> UIColor {
var componentColor: UIColor = .white
if let componentColorsCount = dataSource?.numberOfComponentColors() {
let compoenetColorIndex = index % componentColorsCount
componentColor = dataSource?.pieChartComponentColor(at: compoenetColorIndex) ?? .white
}

return componentColor
}

override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let location = touches.first?.location(in: self) else { return }
guard let component = selectedComponent else { return }
func getLineColor(index: Int) -> UIColor {
var lineColor: UIColor = .black
if let lineColorsCount = dataSource?.numberOfLineColors?() {
let lineColorIndex = index % lineColorsCount
lineColor = dataSource?.pieChartLineColor?(at: lineColorIndex) ?? .black
}

if component.containsPoint(point: location) {
delegate?.didSelectedPieChartComponent?(at: component.index!)
return lineColor
}

func getSubView(index: Int) -> UIView? {
var subView: UIView?
if let subViewsCount = dataSource?.numberOfComponentSubViews?() {
let subViewIndex = index % subViewsCount
subView = dataSource?.pieChartComponentSubView?(at: subViewIndex)
}

component.stopAnimation(animationType: seletingAnimationType)
selectedComponent = nil
return subView
}
}

public extension CSPieChart {
public func reloadPieChart() {

func clear() {
layer.sublayers?.removeAll()

setNeedsDisplay()
componentList.removeAll()
}
}

0 comments on commit 11dc208

Please sign in to comment.