1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- //
- // AccountLoadingView.swift
- // PDF Reader Pro
- //
- // Created by User-Tangchao on 2024/11/27.
- //
- import Cocoa
- class ConnectImageView: NSImageView {
- lazy var contentLayer:CALayer = {
- let imgLayer = CALayer()
- imgLayer.frame = self.layer?.bounds ?? .zero
- let image = NSImage(named: "KMImageNameAccountRefreshLoading")!
- imgLayer.contents = image.layerContents(forContentsScale: self.layer!.contentsScale)
- imgLayer.contentsScale = self.layer?.contentsScale ?? 0
- return imgLayer
- }()
-
- func startCircleAnimtion(clockwise:Bool = true) {
- wantsLayer = true
- contentLayer.frame = bounds
- layer?.addSublayer(contentLayer)
- // print("layer archPoint:\(layer?.anchorPoint),new layer anchorPoint:\(newLayer.anchorPoint)")
- // 注意⚠️,修改了anchorPoint会变更frame,无法实现预期在效果。在macOS上anchorPoint默认为(0,0),如果是新建一个Layer,则layer的anchorPoint默认为(0.5,0.5)
- // 在拉伸视图时,默认layer的frame以及anchorPoint都发生了变化,造成动画不是预期的样子。
- // 所以最终的解决方案是,单独新建一个子Layer,用来处理动画
- let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
- let from:CGFloat = CGFloat.pi * 2
- let end:CGFloat = 0
- rotateAnimation.fromValue = clockwise ? from : end
- rotateAnimation.toValue = clockwise ? end : from
- rotateAnimation.duration = 1.5
- rotateAnimation.isAdditive = true
- rotateAnimation.repeatDuration = CFTimeInterval.infinity
- contentLayer.add(rotateAnimation, forKey: "rotateAnimation")
- }
-
- func stopCircleAnimation() {
- contentLayer.removeAnimation(forKey: "rotateAnimation")
- contentLayer.removeFromSuperlayer()
- }
- }
- class AccountLoadingView: NSView {
- private lazy var contentBox_: NSBox = {
- let view = NSBox()
- view.boxType = .custom
- view.titlePosition = .noTitle
- view.contentViewMargins = .zero
- view.borderWidth = 0
- return view
- }()
-
- private lazy var iconIv_: ConnectImageView = {
- let view = ConnectImageView()
- // view.image = NSImage(named: "KMImageNameAccountRefreshLoading")
- return view
- }()
-
- convenience init() {
- self.init(frame: .init(x: 0, y: 0, width: 160, height: 118))
-
- self.initSubviews()
- }
-
- override func awakeFromNib() {
- super.awakeFromNib()
-
- self.initSubviews()
- }
-
- func initSubviews() {
- self.addSubview(self.contentBox_)
- self.contentBox_.km_add_inset_constraint()
-
- self.contentBox_.contentView?.addSubview(self.iconIv_)
- self.iconIv_.km_add_size_constraint(size: .init(width: 40, height: 40))
- self.iconIv_.km_add_centerX_constraint()
- self.iconIv_.km_add_centerY_constraint()
-
- self.contentBox_.fillColor = .black
- self.contentBox_.cornerRadius = 10
- }
-
- func startAnimation() {
- DispatchQueue.main.async {
- self.iconIv_.startCircleAnimtion()
- }
- }
-
- func stopAnimation() {
- self.iconIv_.stopCircleAnimation()
- }
- }
|