暫無描述

AlertViewAnimatable.swift 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // AlertAnimator.swift
  3. // PaiAi
  4. //
  5. // Created by ffib on 2018/12/17.
  6. // Copyright © 2018 yb. All rights reserved.
  7. //
  8. import UIKit
  9. protocol AlertViewAnimatable {
  10. func toViewPresentedAnimation(duration: TimeInterval, in view: UIView)
  11. func fromViewPresentingAnimation(duration: TimeInterval, in view: UIView)
  12. func contentViewAppearAnimation(duration: TimeInterval, in view: UIView)
  13. func contentViewDisappearAnimation(duration: TimeInterval, in view: UIView)
  14. }
  15. extension AlertViewAnimatable {
  16. func toViewPresentedAnimation(duration: TimeInterval, in view: UIView) {
  17. let animation = CABasicAnimation(keyPath: "backgroundColor")
  18. animation.duration = duration
  19. animation.isRemovedOnCompletion = true
  20. animation.toValue = UIColor(red: 52 / 255,
  21. green: 52 / 255,
  22. blue: 52 / 255,
  23. alpha: 0.7).cgColor
  24. animation.fromValue = UIColor(red: 52 / 255,
  25. green: 52 / 255,
  26. blue: 52 / 255,
  27. alpha: 0).cgColor
  28. view.layer.add(animation, forKey: nil)
  29. }
  30. func fromViewPresentingAnimation(duration: TimeInterval, in view: UIView) {
  31. let animation = CABasicAnimation(keyPath: "backgroundColor")
  32. animation.duration = duration
  33. animation.isRemovedOnCompletion = false
  34. animation.fillMode = .forwards
  35. animation.toValue = UIColor(red: 52 / 255,
  36. green: 52 / 255,
  37. blue: 52 / 255,
  38. alpha: 0).cgColor
  39. animation.fromValue = UIColor(red: 52 / 255,
  40. green: 52 / 255,
  41. blue: 52 / 255,
  42. alpha: 0.7).cgColor
  43. view.layer.add(animation, forKey: nil)
  44. }
  45. }