ImagesAnimateScrollView.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // ImagesAnimateScrollView.swift
  3. // ContactPoster
  4. //
  5. // Created by TSYH on 2024/3/4.
  6. //
  7. import UIKit
  8. class ImagesAnimateScrollView: UIView {
  9. enum `Direction` {
  10. case leftToRight
  11. case rightToLeft
  12. case topToBottom
  13. case bottomToTop
  14. }
  15. lazy var imageView1 = UIImageView()
  16. lazy var imageView2 = UIImageView()
  17. var animationImageName: String? {
  18. didSet {
  19. if let imageName = animationImageName,
  20. let image = UIImage(named: imageName) {
  21. imageView1.image = image
  22. imageView2.image = image
  23. imageView1.frame.size = image.size
  24. imageView2.frame.size = image.size
  25. imageView1.contentMode = .scaleAspectFill
  26. imageView2.contentMode = .scaleAspectFill
  27. }
  28. }
  29. }
  30. lazy var direction: `Direction` = .leftToRight
  31. lazy var margin: CGFloat = 16.0
  32. override init(frame: CGRect) {
  33. super.init(frame: frame)
  34. setupObser()
  35. }
  36. required init?(coder: NSCoder) {
  37. super.init(coder: coder)
  38. setupObser()
  39. }
  40. func setupObser() {
  41. clipsToBounds = true
  42. NotificationCenter.default.addObserver(self, selector: #selector(onAppActiveNotify(_:)), name: UIApplication.didBecomeActiveNotification, object: nil)
  43. }
  44. @objc func onAppActiveNotify(_ notify: Notification) {
  45. DispatchQueue.main.async {
  46. self.startAnimation()
  47. }
  48. }
  49. func setupSubviews() {
  50. if imageView1.superview == nil {
  51. addSubview(imageView1)
  52. addSubview(imageView2)
  53. switch direction {
  54. case .leftToRight:
  55. imageView1.x = width-imageView1.width
  56. imageView1.y = 0
  57. imageView2.y = 0
  58. imageView2.x = imageView1.x-imageView2.width-margin
  59. case .rightToLeft:
  60. imageView1.x = 0
  61. imageView1.y = 0
  62. imageView2.y = 0
  63. imageView2.x = imageView1.width+margin
  64. case .topToBottom:
  65. imageView1.x = 0
  66. imageView1.y = height-imageView1.height
  67. imageView2.x = 0
  68. imageView2.y = imageView1.y-imageView2.height-margin
  69. case .bottomToTop:
  70. imageView1.x = 0
  71. imageView1.y = 0
  72. imageView2.x = 0
  73. imageView2.y = imageView1.height+margin
  74. }
  75. }
  76. }
  77. func startAnimation() {
  78. setupSubviews()
  79. for iv in [imageView1, imageView2] {
  80. iv.layer.removeAllAnimations()
  81. }
  82. var moveAnim = CABasicAnimation(keyPath: "position.x")
  83. switch direction {
  84. case .leftToRight:
  85. moveAnim.byValue = imageView1.width
  86. case .rightToLeft:
  87. moveAnim.byValue = -imageView1.width
  88. case .topToBottom:
  89. moveAnim = CABasicAnimation(keyPath: "position.y")
  90. moveAnim.byValue = imageView1.height
  91. case .bottomToTop:
  92. moveAnim = CABasicAnimation(keyPath: "position.y")
  93. moveAnim.byValue = -imageView1.height
  94. }
  95. moveAnim.duration = 36
  96. moveAnim.repeatCount = .greatestFiniteMagnitude
  97. imageView1.layer.add(moveAnim, forKey: "move")
  98. imageView2.layer.add(moveAnim, forKey: "move")
  99. }
  100. }