KMNCustomAlertView.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // KMNCustomAlertView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/10/14.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMNCustomAlertView: NSView {
  10. override func draw(_ dirtyRect: NSRect) {
  11. super.draw(dirtyRect)
  12. }
  13. static func alertView(message: String, type: ComponentMessageType, fromView supverView: NSView, point: NSPoint) -> ComponentMessage? {
  14. let properties: ComponentMessageProperty = ComponentMessageProperty(messageType: type, title: message)
  15. let componentMessage = ComponentMessage()
  16. componentMessage.properties = properties
  17. let offsetSize = NSSize(width: componentMessage.properties.propertyInfo.viewWidth, height: componentMessage.properties.propertyInfo.viewHeight)
  18. componentMessage.frame = NSRect(x: (point.x - offsetSize.width/2),
  19. y: (point.y - offsetSize.height/2),
  20. width: offsetSize.width, height: offsetSize.height)
  21. componentMessage.reloadData()
  22. supverView.addSubview(componentMessage)
  23. componentMessage.alphaValue = 0.0
  24. NSAnimationContext.runAnimationGroup({ context in
  25. context.duration = 0.3 // 设置动画持续时间
  26. context.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  27. componentMessage.animator().alphaValue = 1.0 // 动画化透明度
  28. }, completionHandler: {
  29. })
  30. DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
  31. NSAnimationContext.runAnimationGroup({ context in
  32. context.duration = 0.3 // 设置动画持续时间
  33. context.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  34. componentMessage.animator().alphaValue = 0.0 // 动画化透明度
  35. }, completionHandler: {
  36. componentMessage.removeFromSuperview()
  37. })
  38. }
  39. return componentMessage
  40. }
  41. }