You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
3.0 KiB

  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2016 Luke Zhao <me@lkzhao.com>
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import UIKit
  23. @available(iOS 10, tvOS 10, *)
  24. internal class HeroViewPropertyViewContext: HeroAnimatorViewContext {
  25. var viewPropertyAnimator: UIViewPropertyAnimator!
  26. var endEffect: UIVisualEffect?
  27. var startEffect: UIVisualEffect?
  28. override class func canAnimate(view: UIView, state: HeroTargetState, appearing: Bool) -> Bool {
  29. return view is UIVisualEffectView && state.opacity != nil
  30. }
  31. override func resume(timePassed: TimeInterval, reverse: Bool) -> TimeInterval {
  32. guard let visualEffectView = snapshot as? UIVisualEffectView else { return 0 }
  33. if reverse {
  34. viewPropertyAnimator?.stopAnimation(false)
  35. viewPropertyAnimator?.finishAnimation(at: .current)
  36. viewPropertyAnimator = UIViewPropertyAnimator(duration: duration, curve: .linear) {
  37. visualEffectView.effect = reverse ? self.startEffect : self.endEffect
  38. }
  39. }
  40. viewPropertyAnimator.startAnimation()
  41. return duration
  42. }
  43. override func seek(timePassed: TimeInterval) {
  44. viewPropertyAnimator?.pauseAnimation()
  45. viewPropertyAnimator?.fractionComplete = CGFloat(timePassed / duration)
  46. }
  47. override func clean() {
  48. super.clean()
  49. viewPropertyAnimator?.stopAnimation(false)
  50. viewPropertyAnimator?.finishAnimation(at: .current)
  51. viewPropertyAnimator = nil
  52. }
  53. override func startAnimations() -> TimeInterval {
  54. guard let visualEffectView = snapshot as? UIVisualEffectView else { return 0 }
  55. let appearedEffect = visualEffectView.effect
  56. let disappearedEffect = targetState.opacity == 0 ? nil : visualEffectView.effect
  57. startEffect = appearing ? disappearedEffect : appearedEffect
  58. endEffect = appearing ? appearedEffect : disappearedEffect
  59. visualEffectView.effect = startEffect
  60. viewPropertyAnimator = UIViewPropertyAnimator(duration: duration, curve: .linear) {
  61. visualEffectView.effect = self.endEffect
  62. }
  63. viewPropertyAnimator.startAnimation()
  64. return duration
  65. }
  66. }