Skip to content

Instantly share code, notes, and snippets.

@aybekckaya
Last active June 3, 2019 20:39
Show Gist options
  • Save aybekckaya/250c89d70692337f39936a93ee5bfc33 to your computer and use it in GitHub Desktop.
Save aybekckaya/250c89d70692337f39936a93ee5bfc33 to your computer and use it in GitHub Desktop.
// Animation values creator (1)
struct PulseConfiguration {
// static values
private var scaleChangeAmount:CGFloat = 0.015
private var maxScale:CGFloat = 1.5
private var minScale:CGFloat = 1
// dynamic Values
private var currentScale:CGFloat = 1
private var isExpanding:Bool = true
init(scaleChangeAmount:CGFloat = 0.015 , minScale:CGFloat = 1.5 , maxScale:CGFloat = 1) {
self.scaleChangeAmount = scaleChangeAmount
self.minScale = minScale
self.maxScale = maxScale
}
mutating func scale(_currentScale:CGFloat? = nil)->CGFloat {
if let sc = _currentScale { currentScale = sc }
let scaleChangeValue = isExpanding == true ? scaleChangeAmount : (-1) * scaleChangeAmount
currentScale += scaleChangeValue
if currentScale >= maxScale { isExpanding = false }
else if currentScale <= minScale { isExpanding = true }
return currentScale
}
}
private var currentPulseConfig:PulseConfiguration = PulseConfiguration()
displayLink?.displayLinkUpdated = { timeInterval in
...
self.updatePulsingLayer(timeInterval: timeInterval)
}
private func updatePulsingLayer(timeInterval:CFTimeInterval) {
pulsatingLayer.transform = CATransform3DMakeScale(currentPulseConfig.scale(), currentPulseConfig.scale(), 1)
}
// Tests
//(2a)
func test1PulsatingAnimationWorksCoool() {
var pulsatingConfig = PulseConfiguration(scaleChangeAmount: 0.5, minScale: 1, maxScale: 2)
var scale = pulsatingConfig.scale(_currentScale: 1)
XCTAssert(scale == 1.5)
scale = pulsatingConfig.scale()
scale = pulsatingConfig.scale()
XCTAssert(scale == 1.5)
scale = pulsatingConfig.scale()
scale = pulsatingConfig.scale()
XCTAssert(scale == 1.5)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment