Guest User

Untitled

a guest
Nov 13th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. //
  2. // CAAnimationHelpers.swift
  3. // ChainingAnimations
  4. //
  5. // Created by Patrik Sjöberg on 2018-11-13.
  6. // Copyright © 2018 Patrik Sjöberg. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10. import UIKit
  11.  
  12. /**
  13. Create a keyframe animation for `keyPath` with values supplied in `values`.
  14.  
  15. Timings will be calculated from the `track` string that is built of space and numbers,
  16. where the numbers must correspond to an index in the `values` list.
  17.  
  18. Example: `animation(track: "0 1 1", keyPath: "opacity", values: [0.4, 0.8])` will
  19. result in one keypoint where opaciry is 0.4 at time 0, 0.8 at time 0.5 and 0.8 at time 1,
  20. creating a blink effect where opacity is higher for a longer time than it's lower.
  21. */
  22. func animation<T>(track: String, for keyPath: String, values: [T]) -> CAAnimation {
  23. let valuesAndTimes = valuesAndTimesFrom(track: track, values: values)
  24. return animation(keyPath: keyPath, valuesAndTimes: valuesAndTimes)
  25. }
  26.  
  27. /**
  28. Play animations in a sequence
  29.  
  30. Creates a group with supplied animations and durations
  31. Duration is set for each animation and then the total of all the animations is set to the group
  32. */
  33. public func sequential(animationsAndDurations: [(CAAnimation, TimeInterval)]) -> CAAnimation {
  34. let group = CAAnimationGroup()
  35.  
  36. var totalDuration: TimeInterval = 0
  37. for (anim, duration) in animationsAndDurations {
  38. anim.beginTime = totalDuration
  39. anim.duration = duration
  40. totalDuration += duration
  41. }
  42. group.duration = totalDuration
  43. group.animations = animationsAndDurations.map({$0.0})
  44.  
  45. return group
  46. }
  47.  
  48. /**
  49. Play animations concurrently
  50.  
  51. Creates a group of animations
  52. */
  53. func concurrent(animations: [CAAnimation]) -> CAAnimation {
  54. let group = CAAnimationGroup()
  55. group.animations = animations
  56. return group
  57. }
  58.  
  59. fileprivate func animation<T>(keyPath: String, valuesAndTimes: [(T, TimeInterval)]) -> CAAnimation {
  60. let anim = CAKeyframeAnimation(keyPath: keyPath)
  61. anim.values = valuesAndTimes.map{$0.0}
  62. anim.keyTimes = valuesAndTimes.map{$0.1}.map({NSNumber(value: $0)})
  63. anim.calculationMode = .cubic
  64. return anim
  65. }
  66.  
  67.  
  68. fileprivate func valuesAndTimesFrom<T>(track: String, values: [T]) -> [(T, TimeInterval)] {
  69. let length = track.count - 1
  70. return track.enumerated().compactMap { indexAndChar -> (T, TimeInterval)? in
  71. let (index, char) = indexAndChar
  72. if let valueIndex = Int(String(char)) {
  73. let value = values[valueIndex]
  74. let time = TimeInterval(index) / TimeInterval(length)
  75. return (value, time)
  76. }
  77. return nil
  78. }
  79. }
Add Comment
Please, Sign In to add comment