-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from f8th/master
Added rate animations
- Loading branch information
Showing
6 changed files
with
170 additions
and
60 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// Gauge+Animations.swift | ||
// GaugeKit | ||
// | ||
// Created by David Pelletier on 16-03-30. | ||
// Copyright © 2016 Petr Korolev. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
extension Gauge { | ||
public func animateRate(duration: NSTimeInterval, newValue: CGFloat, completion: (Bool) -> ()) -> Void { | ||
animationTimer.invalidate() | ||
|
||
let refreshRate: Double = 0.1 | ||
let rateSpeed: CGFloat = CGFloat(refreshRate) * ((newValue - self.rate) / CGFloat(duration)) | ||
print(rateSpeed) | ||
|
||
animationTimer = NSTimer.scheduledTimerWithTimeInterval( | ||
refreshRate, | ||
target: self, | ||
selector: #selector(updateProgress), | ||
userInfo: [newValue, rateSpeed], | ||
repeats: true | ||
) | ||
|
||
animationTimer.fire() | ||
|
||
animationCompletionBlock = completion | ||
} | ||
|
||
func updateProgress(let timer: NSTimer) -> Void { | ||
let userInfo = timer.userInfo as! [CGFloat] | ||
guard let newValue: CGFloat = userInfo.first! else { | ||
print("GAUGE-KIT: Error, new value not defined...") | ||
return | ||
} | ||
|
||
guard let rateSpeed: CGFloat = userInfo.last else { | ||
print("GAUGE-KIT: Error, rate speed could not be defined...") | ||
return | ||
} | ||
|
||
self.rate += rateSpeed | ||
|
||
if rateSpeed < 0 { | ||
if self.rate <= newValue { | ||
self.rate = newValue | ||
timer.invalidate() | ||
animationCompletionBlock(true) | ||
//print("GAUGE-KIT: Gauge went down to \(newValue)") | ||
} | ||
|
||
if self.rate <= 0 { | ||
self.rate = 0 | ||
timer.invalidate() | ||
animationCompletionBlock(true) | ||
//print("GAUGE-KIT: Gauge emptied") | ||
} | ||
} | ||
|
||
if rateSpeed >= 0 { | ||
if self.rate >= newValue { | ||
self.rate = newValue | ||
timer.invalidate() | ||
animationCompletionBlock(true) | ||
//print("GAUGE-KIT: Gauge went up to \(newValue)") | ||
} | ||
|
||
if self.rate >= self.maxValue { | ||
self.rate = self.maxValue | ||
timer.invalidate() | ||
animationCompletionBlock(true) | ||
//print("GAUGE-KIT: Gauge filled") | ||
} | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// UIColor+Blend.swift | ||
// SWGauge | ||
// | ||
// Created by David Pelletier on 2015-03-06. | ||
// Copyright (c) 2015 Petr Korolev. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
func + (left: UIColor, right: UIColor) -> UIColor { | ||
var leftRGBA = [CGFloat](count: 4, repeatedValue: 0.0) | ||
var rightRGBA = [CGFloat](count: 4, repeatedValue: 0.0) | ||
|
||
left.getRed(&leftRGBA[0], green: &leftRGBA[1], blue: &leftRGBA[2], alpha: &leftRGBA[3]) | ||
right.getRed(&rightRGBA[0], green: &rightRGBA[1], blue: &rightRGBA[2], alpha: &rightRGBA[3]) | ||
|
||
return UIColor( | ||
red: max(leftRGBA[0], rightRGBA[0]), | ||
green: max(leftRGBA[1], rightRGBA[1]), | ||
blue: max(leftRGBA[2], rightRGBA[2]), | ||
alpha: max(leftRGBA[3], rightRGBA[3]) | ||
) | ||
} | ||
|
||
func * (left: CGFloat, right: UIColor) -> UIColor { | ||
var rightRGBA = [CGFloat](count: 4, repeatedValue: 0.0) | ||
|
||
right.getRed(&rightRGBA[0], green: &rightRGBA[1], blue: &rightRGBA[2], alpha: &rightRGBA[3]) | ||
|
||
return UIColor( | ||
red: rightRGBA[0] * left, | ||
green: rightRGBA[1] * left, | ||
blue: rightRGBA[2] * left, | ||
alpha: rightRGBA[3] | ||
) | ||
} | ||
|
||
func * (left: UIColor, right: CGFloat) -> UIColor { | ||
return right * left | ||
} |