Commit fb34be35 by Dmitriy Stepanets

Working on dash line percent

parent 73bc486a
...@@ -7,43 +7,59 @@ ...@@ -7,43 +7,59 @@
import UIKit import UIKit
private struct CircleSegment {
let chord:CGFloat
let height:CGFloat
var radius:CGFloat {
return (height / 2) + (pow(chord,2) / (8 * height))
}
var diameter:CGFloat {
return radius * 2
}
var alpha:CGFloat {
return asin((radius - height) / radius)
}
init(chord:CGFloat, height:CGFloat) {
self.chord = chord
self.height = height
}
}
class CitySunCell: UITableViewCell { class CitySunCell: UITableViewCell {
static let kIdentifier = "citySunCell" static let kIdentifier = "citySunCell"
//Private //Private
//Heading
private let headingLabel = UILabel() private let headingLabel = UILabel()
private let arrowButton = ArrowButton() private let arrowButton = ArrowButton()
//Infographic
private let infographicContainer = UIView() private let infographicContainer = UIView()
private let infographicLabel = UILabel() private let infographicLabel = UILabel()
private let kDashLineTopInset:CGFloat = 14
private let infographicDashLine = CAShapeLayer() private let infographicDashLine = CAShapeLayer()
private let earthImageView = UIImageView(image: UIImage(named: "earth")) private let earthImageView = UIImageView(image: UIImage(named: "earth"))
//Sun
private let sunImageView = UIImageView(image: UIImage(named: "sun")) private let sunImageView = UIImageView(image: UIImage(named: "sun"))
private let sunActivityContainer = UIView() private let sunActivityContainer = UIView()
private let sunActivityStartLabel = UILabel() private let sunActivityStartLabel = UILabel()
private let sunActivityEndLabel = UILabel() private let sunActivityEndLabel = UILabel()
private let maxUvLabel = UILabel() private let maxUvLabel = UILabel()
private var dashLinePoints:[CGPoint] {
let earthImageViewInset:CGFloat = 16
let sideInset:CGFloat = 18
let topInset = earthImageView.frame.origin.y - earthImageViewInset
let a: CGFloat = (infographicContainer.frame.width / 2) - sideInset
let b: CGFloat = earthImageView.frame.height + earthImageViewInset
var points = [CGPoint]()
let tValues = stride(from: -.pi, to: 0, by: 0.02).map{$0}
for t in tValues{
let x = a * cos(CGFloat(t)) + a + sideInset
let y = b * sin(CGFloat(t)) + b + topInset
points.append(CGPoint(x: x, y: y))
}
return points
}
//Computed //Computed
private var dashLinePath:CGPath? private var dashLinePath = UIBezierPath()
private var earthCircleSegment:CircleSegment {
return .init(chord: earthImageView.frame.width,
height: earthImageView.frame.height)
}
private var dashCircleSegment:CircleSegment {
let kSideInset:CGFloat = 17
let earthCircleSegment = self.earthCircleSegment
return CircleSegment(chord: earthCircleSegment.chord + kSideInset * 2,
height: earthCircleSegment.height + kDashLineTopInset)
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier) super.init(style: style, reuseIdentifier: reuseIdentifier)
...@@ -66,29 +82,30 @@ class CitySunCell: UITableViewCell { ...@@ -66,29 +82,30 @@ class CitySunCell: UITableViewCell {
} }
private func drawDashLine() { private func drawDashLine() {
let dashPath = UIBezierPath() let segment = dashCircleSegment
let start = -(.pi - segment.alpha)
for index in 0..<dashLinePoints.count { let end = -segment.alpha
if index == 0 { let percent = (start - end) * 0.4
dashPath.move(to: dashLinePoints[index]) dashLinePath = UIBezierPath(arcCenter: .init(x: infographicContainer.frame.width / 2,
continue y: earthImageView.frame.origin.y + segment.radius - kDashLineTopInset),
} radius: segment.radius,
startAngle: start,
dashPath.addLine(to: dashLinePoints[index]) endAngle: start - percent,
} clockwise: true)
infographicDashLine.path = dashLinePath.cgPath
infographicDashLine.path = dashPath.cgPath
} }
private func updateSunPosition() { public func updateSunPosition() {
sunImageView.frame.origin = .init(x: 36 - sunImageView.frame.width/2, sunImageView.layer.removeAllAnimations()
y: infographicContainer.frame.height - sunImageView.frame.height) sunImageView.frame.origin = .init(x: 20 - sunImageView.frame.width / 2,
y: infographicContainer.frame.height - sunImageView.frame.height / 2)
let animation = CAKeyframeAnimation(keyPath: "position") let animation = CAKeyframeAnimation(keyPath: "position")
animation.path = self.dashLinePath animation.path = self.dashLinePath.cgPath
animation.calculationMode = .paced animation.calculationMode = .paced
animation.duration = 5 animation.duration = 2
animation.rotationMode = .rotateAuto animation.rotationMode = .rotateAuto
animation.isRemovedOnCompletion = true
sunImageView.layer.add(animation, forKey: nil) sunImageView.layer.add(animation, forKey: nil)
} }
...@@ -159,9 +176,8 @@ private extension CitySunCell { ...@@ -159,9 +176,8 @@ private extension CitySunCell {
infographicContainer.addSubview(earthImageView) infographicContainer.addSubview(earthImageView)
earthImageView.snp.makeConstraints { (make) in earthImageView.snp.makeConstraints { (make) in
make.left.right.equalToSuperview().inset(18) make.left.right.equalToSuperview().inset(37)
make.bottom.equalToSuperview() make.bottom.equalToSuperview()
make.height.greaterThanOrEqualTo(100).priority(.low)
} }
//Dash line //Dash line
...@@ -173,9 +189,6 @@ private extension CitySunCell { ...@@ -173,9 +189,6 @@ private extension CitySunCell {
//Sun //Sun
sunImageView.frame = .init(origin: .zero, size: CGSize(width: 28, height: 28)) sunImageView.frame = .init(origin: .zero, size: CGSize(width: 28, height: 28))
if let path = dashLinePath {
sunImageView.center = path.currentPoint
}
sunImageView.contentMode = .scaleAspectFit sunImageView.contentMode = .scaleAspectFit
infographicContainer.addSubview(sunImageView) infographicContainer.addSubview(sunImageView)
} }
......
...@@ -130,7 +130,7 @@ extension TodayViewController: UITableViewDelegate { ...@@ -130,7 +130,7 @@ extension TodayViewController: UITableViewDelegate {
} }
if let sunCell = cell as? CitySunCell { if let sunCell = cell as? CitySunCell {
sunCell.updateSunPosition()
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment