AccountLoadingView.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // AccountLoadingView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/11/27.
  6. //
  7. import Cocoa
  8. class ConnectImageView: NSImageView {
  9. lazy var contentLayer:CALayer = {
  10. let imgLayer = CALayer()
  11. imgLayer.frame = self.layer?.bounds ?? .zero
  12. let image = NSImage(named: "KMImageNameAccountRefreshLoading")!
  13. imgLayer.contents = image.layerContents(forContentsScale: self.layer!.contentsScale)
  14. imgLayer.contentsScale = self.layer?.contentsScale ?? 0
  15. return imgLayer
  16. }()
  17. func startCircleAnimtion(clockwise:Bool = true) {
  18. wantsLayer = true
  19. contentLayer.frame = bounds
  20. layer?.addSublayer(contentLayer)
  21. // print("layer archPoint:\(layer?.anchorPoint),new layer anchorPoint:\(newLayer.anchorPoint)")
  22. // 注意⚠️,修改了anchorPoint会变更frame,无法实现预期在效果。在macOS上anchorPoint默认为(0,0),如果是新建一个Layer,则layer的anchorPoint默认为(0.5,0.5)
  23. // 在拉伸视图时,默认layer的frame以及anchorPoint都发生了变化,造成动画不是预期的样子。
  24. // 所以最终的解决方案是,单独新建一个子Layer,用来处理动画
  25. let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
  26. let from:CGFloat = CGFloat.pi * 2
  27. let end:CGFloat = 0
  28. rotateAnimation.fromValue = clockwise ? from : end
  29. rotateAnimation.toValue = clockwise ? end : from
  30. rotateAnimation.duration = 1.5
  31. rotateAnimation.isAdditive = true
  32. rotateAnimation.repeatDuration = CFTimeInterval.infinity
  33. contentLayer.add(rotateAnimation, forKey: "rotateAnimation")
  34. }
  35. func stopCircleAnimation() {
  36. contentLayer.removeAnimation(forKey: "rotateAnimation")
  37. contentLayer.removeFromSuperlayer()
  38. }
  39. }
  40. class AccountLoadingView: NSView {
  41. private lazy var contentBox_: NSBox = {
  42. let view = NSBox()
  43. view.boxType = .custom
  44. view.titlePosition = .noTitle
  45. view.contentViewMargins = .zero
  46. view.borderWidth = 0
  47. return view
  48. }()
  49. private lazy var iconIv_: ConnectImageView = {
  50. let view = ConnectImageView()
  51. // view.image = NSImage(named: "KMImageNameAccountRefreshLoading")
  52. return view
  53. }()
  54. convenience init() {
  55. self.init(frame: .init(x: 0, y: 0, width: 160, height: 118))
  56. self.initSubviews()
  57. }
  58. override func awakeFromNib() {
  59. super.awakeFromNib()
  60. self.initSubviews()
  61. }
  62. func initSubviews() {
  63. self.addSubview(self.contentBox_)
  64. self.contentBox_.km_add_inset_constraint()
  65. self.contentBox_.contentView?.addSubview(self.iconIv_)
  66. self.iconIv_.km_add_size_constraint(size: .init(width: 40, height: 40))
  67. self.iconIv_.km_add_centerX_constraint()
  68. self.iconIv_.km_add_centerY_constraint()
  69. self.contentBox_.fillColor = .black
  70. self.contentBox_.cornerRadius = 10
  71. }
  72. func startAnimation() {
  73. DispatchQueue.main.async {
  74. self.iconIv_.startCircleAnimtion()
  75. }
  76. }
  77. func stopAnimation() {
  78. self.iconIv_.stopCircleAnimation()
  79. }
  80. }