Created
November 19, 2018 18:27
-
-
Save Coder-ACJHP/87753d8d30b9cb0c5102c753d971332c 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
// | |
// ViewController.swift | |
// CountAnm | |
// | |
// Created by Onur Işık on 19.11.2018. | |
// Copyright © 2018 Onur Işık. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
var elapsedTime:Double = 0.0 | |
private var startValue: Double = 0.0 | |
private var endValue: Double = 1000 | |
private let animationDuration = 2.5 | |
private var animationStartDate: Date! | |
@IBOutlet weak var slider: UISlider! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
func startCountUp() { | |
elapsedTime = 0.0 | |
animationStartDate = Date() | |
// create displayLink & add it to the run-loop | |
let displayLink = CADisplayLink(target: self, selector: #selector(handleCountUp)) | |
displayLink.add(to: .main, forMode: .default) | |
} | |
func startCountDown() { | |
elapsedTime = 0.0 | |
animationStartDate = Date() | |
// create displayLink & add it to the run-loop | |
let displayLink = CADisplayLink(target: self, selector: #selector(handleCountDown)) | |
displayLink.add(to: .main, forMode: .default) | |
} | |
@objc fileprivate func handleCountUp(_ displayLink: CADisplayLink) { | |
let now = Date() | |
elapsedTime = now.timeIntervalSince(animationStartDate) | |
if elapsedTime < animationDuration { | |
let percentage = elapsedTime / animationDuration | |
let value = percentage * (endValue - startValue) | |
self.slider.setValue(Float(value / 1000), animated: true) | |
} else { | |
displayLink.invalidate() | |
self.startCountDown() | |
} | |
} | |
@objc fileprivate func handleCountDown(_ displayLink: CADisplayLink) { | |
let now = Date() | |
elapsedTime = now.timeIntervalSince(animationStartDate) | |
if elapsedTime < animationDuration { | |
let percentage = elapsedTime / animationDuration | |
let value = percentage * (endValue - startValue) | |
let sliderValue = self.slider.value - Float(value / 15000) | |
self.slider.setValue(sliderValue, animated: true) | |
} else { | |
displayLink.invalidate() | |
} | |
} | |
@IBAction func startPressed(_ sender: Any) { | |
self.startCountUp() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment