Created
August 26, 2019 16:33
-
-
Save amixpal/93491a4170bd963fd31240e03eb9a007 to your computer and use it in GitHub Desktop.
Sample Code
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
// | |
// NotificationOnGoingWorkoutViewController.swift | |
// Fitme | |
// | |
// Created by Paly on 6/27/19. | |
// Copyright © 2019 Heeral. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
import CoreFoundation | |
class NotificationOnGoingWorkoutViewController: UIViewController | |
{ | |
let shapeLayer = CAShapeLayer() | |
var countdownTimer = Timer() | |
@IBOutlet var closeButton: UIButton! | |
var appointmentId: String! | |
var workoutNameDetail: String! | |
var workoutImageData: String! | |
var trainerId: String! | |
var startTime: String! | |
var trainerName: String! | |
var totalTime = 0 | |
@IBOutlet weak var currentDate: UILabel! | |
@IBOutlet weak var timeLeft: UILabel! | |
@IBOutlet weak var completeWorkoutButton: UIButton! | |
@IBOutlet var workoutName: UILabel! | |
@IBOutlet var workoutImage: UIImageView! | |
override func viewDidLoad() | |
{ | |
super.viewDidLoad() | |
closeButton.contentHorizontalAlignment = .fill | |
closeButton.contentVerticalAlignment = .fill | |
bindData() | |
let radius = getRadius() | |
let strokeWidth = Int(0.20 * radius) | |
InitializeView(radius: radius, strokeWidth: CGFloat(strokeWidth)) | |
InternalViews(radius: radius, strokeWidth: CGFloat(strokeWidth)) | |
countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true) | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
self.navigationController?.setNavigationBarHidden(true , animated: false) | |
} | |
override func viewWillDisappear(_ animated: Bool) { | |
super.viewWillDisappear(animated) | |
self.navigationController?.setNavigationBarHidden(false, animated: false) | |
} | |
func bindData() { | |
workoutName.text = workoutNameDetail + " with " + trainerName | |
workoutImage.sd_setImage(with: URL(string: workoutImageData != nil ? workoutImageData! : ""), placeholderImage: UIImage.init(named: "fifth_onboarding"), options: .handleCookies, completed: nil) | |
let serverTime = Date(millisecondsSince1970: Int64(self.startTime)!) | |
let elapsedMinutes = Date().timeIntervalSince(serverTime) / 60 | |
totalTime = Int(elapsedMinutes) | |
totalTime = (60 - totalTime) * 60 | |
startTimer() | |
} | |
@IBAction func closeButtonClicked(_ sender: Any) { | |
let rootViewController = MainTabBarViewController() | |
if let appDelegate = UIApplication.shared.delegate as? AppDelegate { | |
appDelegate.window?.rootViewController = rootViewController | |
} | |
} | |
func InitializeView(radius:CGFloat, strokeWidth:CGFloat) | |
{ | |
completeWorkoutButton.layer.cornerRadius = 8 | |
completeWorkoutButton.layer.masksToBounds = true | |
completeWorkoutButton.isEnabled = false | |
let date = Date() | |
let formatter = DateFormatter() | |
formatter.dateFormat = "MMMM dd, yyyy" | |
let result = formatter.string(from: date) | |
currentDate.text = result | |
// drawing track | |
let circulerPath = UIBezierPath(arcCenter: view.center, radius: radius, startAngle: -CGFloat.pi , endAngle: 2 * CGFloat.pi, clockwise: true) | |
let trackLayer = CAShapeLayer() | |
trackLayer.path = circulerPath.cgPath | |
trackLayer.strokeColor = CustomColors().transparentWhiteColor.cgColor | |
trackLayer.lineWidth = strokeWidth | |
trackLayer.fillColor = UIColor.clear.cgColor | |
trackLayer.lineCap = CAShapeLayerLineCap.round | |
view.layer.addSublayer(trackLayer) | |
// drawig a circle | |
shapeLayer.path = circulerPath.cgPath | |
shapeLayer.strokeColor = UIColor.white.cgColor | |
shapeLayer.lineWidth = strokeWidth | |
shapeLayer.fillColor = UIColor.clear.cgColor | |
shapeLayer.lineCap = CAShapeLayerLineCap.round | |
shapeLayer.strokeEnd = 0 | |
view.layer.addSublayer(shapeLayer) | |
} | |
func InternalViews(radius:CGFloat, strokeWidth:CGFloat) | |
{ | |
var alpha:CGFloat = 0 | |
//Fix for iPhone SE | |
if (view.bounds.size.height < 600) | |
{ | |
alpha = 4 | |
} | |
//adding neck view above circular view | |
let neckWidth = 24 | |
let neckHeight = 16 | |
let neckX = view.center.x - CGFloat(neckWidth/2) | |
let neckY = view.center.y - radius - strokeWidth - alpha | |
let neckView = UIView(frame: CGRect(x: neckX, y: neckY, width: CGFloat(neckWidth), height: CGFloat(neckHeight))) | |
view.addSubview(neckView) | |
neckView.layer.backgroundColor = UIColor.white.cgColor | |
//adding cap view above neck view | |
let capWidth = 40 | |
let capHeight = 16 | |
let capX = view.center.x - CGFloat(capWidth/2) | |
let capY = view.center.y - radius - strokeWidth - CGFloat(neckHeight) - alpha | |
let capView = UIView(frame: CGRect(x: capX, y: capY, width: CGFloat(capWidth), height: CGFloat(capHeight))) | |
view.addSubview(capView) | |
capView.layer.backgroundColor = UIColor.white.cgColor | |
capView.layer.cornerRadius = 8 | |
capView.layer.masksToBounds = true | |
//adding twelve and six view | |
let constant = 10 | |
let verticleWidth = 5 | |
let verticleHeight = 12 | |
let verticleX = view.center.x - CGFloat(verticleWidth/2) | |
let twelveY = view.center.y - radius + CGFloat(verticleHeight + constant) | |
let atTwelveView = UIView(frame: CGRect(x: verticleX, y: twelveY, width: CGFloat(verticleWidth), height: CGFloat(verticleHeight))) | |
view.addSubview(atTwelveView) | |
atTwelveView.layer.backgroundColor = CustomColors().transparentWhiteColor.cgColor | |
atTwelveView.layer.cornerRadius = 4 | |
let sixY = view.center.y + radius - strokeWidth - CGFloat(constant) | |
let atSixView = UIView(frame: CGRect(x: verticleX, y: sixY, width: CGFloat(verticleWidth), height: CGFloat(verticleHeight))) | |
view.addSubview(atSixView) | |
atSixView.layer.backgroundColor = CustomColors().transparentWhiteColor.cgColor | |
atSixView.layer.cornerRadius = 4 | |
//adding three and nine view | |
let horizontalWidth = 12 | |
let horizontalHeight = 5 | |
let horizontalY = view.center.y - CGFloat(horizontalHeight/2) | |
let threeX = view.center.x + radius - strokeWidth - CGFloat(constant) | |
let atThreeView = UIView(frame: CGRect(x: threeX, y: horizontalY, width: CGFloat(horizontalWidth), height: CGFloat(horizontalHeight))) | |
view.addSubview(atThreeView) | |
atThreeView.layer.backgroundColor = CustomColors().transparentWhiteColor.cgColor | |
atThreeView.layer.cornerRadius = 4 | |
let nineX = view.center.x - radius + CGFloat(horizontalWidth + constant) | |
let atNineView = UIView(frame: CGRect(x: nineX, y: horizontalY, width: CGFloat(horizontalWidth), height: CGFloat(horizontalHeight))) | |
view.addSubview(atNineView) | |
atNineView.layer.backgroundColor = CustomColors().transparentWhiteColor.cgColor | |
atNineView.layer.cornerRadius = 4 | |
} | |
func startTimer() | |
{ | |
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd") | |
basicAnimation.toValue = 1 | |
basicAnimation.duration = CFTimeInterval(totalTime) | |
basicAnimation.speed = 0.65 | |
basicAnimation.fillMode = CAMediaTimingFillMode.forwards | |
basicAnimation.isRemovedOnCompletion = false | |
shapeLayer.add(basicAnimation, forKey: "URSoBasic") | |
} | |
@objc func updateTime() { | |
timeLeft.text = "\(timeFormatted(totalTime))" | |
if totalTime != 0 { | |
totalTime -= 1 | |
} else { | |
endTimer() | |
} | |
} | |
func endTimer() { | |
completeWorkoutButton.isEnabled = true | |
} | |
func getRadius() -> CGFloat | |
{ | |
var radius:Int = 0 | |
let width = view.bounds.size.width | |
radius = Int((width - (2*50)) / 2) | |
return CGFloat(radius) | |
} | |
func timeFormatted(_ totalSeconds: Int) -> String { | |
let seconds: Int = totalSeconds % 60 | |
let minutes: Int = (totalSeconds / 60) % 60 | |
return String(format: "%02d:%02d", minutes, seconds) | |
} | |
} | |
extension Date { | |
var millisecondsSince1970: Int64 { | |
return Int64(Int(timeIntervalSince1970 * 1000)) | |
} | |
init(millisecondsSince1970: Int64) | |
{ | |
self.init(timeIntervalSince1970: TimeInterval(millisecondsSince1970)/1000) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment