找回密码
 注册
搜索
系统gho:最纯净好用系统下载站投放广告、加入VIP会员,请联系 微信:wuyouceo
查看: 83|回复: 2

[原创] 用swift手搓鼠标效果,方便录屏。

[复制链接]
发表于 昨天 22:25 | 显示全部楼层 |阅读模式
本帖最后由 seekyou2008 于 2026-5-20 22:28 编辑

之前剪demo发现鼠标效果太差了,于是用swift手搓了一个,参考了chrome模拟移动端界面里鼠标效果。整体看起来灰色加透明,还有一点放大镜的感觉。效果如下:




代码如下:

  1. import Cocoa
  2. import Foundation
  3. import QuartzCore

  4. class CursorOverlay: NSObject {
  5.     var cursorWindow: NSWindow!
  6.     var cursorLayer: CALayer!
  7.     let rectSize: CGFloat = 40.0 // 40x40 像素,大小非常克制、高级
  8.    
  9.     override init() {
  10.         super.init()
  11.         setupCursorWindow()
  12.         setupEventMonitors()
  13.     }
  14.    
  15.     func setupCursorWindow() {
  16.         let rect = NSRect(x: 0, y: 0, width: rectSize, height: rectSize)
  17.         
  18.         cursorWindow = NSWindow(contentRect: rect,
  19.                                 styleMask: .borderless,
  20.                                 backing: .buffered,
  21.                                 defer: false)
  22.         cursorWindow.isOpaque = false
  23.         cursorWindow.backgroundColor = .clear
  24.         cursorWindow.level = .screenSaver
  25.         cursorWindow.ignoresMouseEvents = true
  26.         cursorWindow.hasShadow = false
  27.         
  28.         let view = NSView(frame: rect)
  29.         view.wantsLayer = true
  30.         
  31.         // 绘制完全体深空灰半透明圆圈
  32.         cursorLayer = CALayer()
  33.         cursorLayer.frame = rect
  34.         cursorLayer.anchorPoint = CGPoint(x: 0.5, y: 0.5) // 强制锁死动画锚点在正中心
  35.         
  36.         // 颜色调教:比普通灰色更高级的深空灰 (rgba(80, 80, 80, 0.45))
  37.         cursorLayer.backgroundColor = NSColor(white: 0.31, alpha: 0.45).cgColor
  38.         cursorLayer.cornerRadius = rectSize / 2
  39.         
  40.         // ✨ 新增:高光防爆边框 (1.5 像素宽,稍微亮一点的半透明灰)
  41.         cursorLayer.borderWidth = 1.5
  42.         cursorLayer.borderColor = NSColor(white: 0.5, alpha: 0.4).cgColor
  43.         
  44.         view.layer?.addSublayer(cursorLayer)
  45.         cursorWindow.contentView = view
  46.         cursorWindow.makeKeyAndOrderFront(nil)
  47.         updateCursorPosition()
  48.     }
  49.    
  50.     func setupEventMonitors() {
  51.         NSEvent.addGlobalMonitorForEvents(matching: [.mouseMoved, .leftMouseDragged]) { [weak self] _ in self?.updateCursorPosition() }
  52.         NSEvent.addLocalMonitorForEvents(matching: [.mouseMoved, .leftMouseDragged]) { [weak self] event in
  53.             self?.updateCursorPosition()
  54.             return event
  55.         }
  56.         
  57.         NSEvent.addGlobalMonitorForEvents(matching: [.leftMouseDown]) { [weak self] _ in self?.animateDown() }
  58.         NSEvent.addLocalMonitorForEvents(matching: [.leftMouseDown]) { [weak self] event in
  59.             self?.animateDown()
  60.             return event
  61.         }
  62.         
  63.         NSEvent.addGlobalMonitorForEvents(matching: [.leftMouseUp]) { [weak self] _ in self?.animateUp() }
  64.         NSEvent.addLocalMonitorForEvents(matching: [.leftMouseUp]) { [weak self] event in
  65.             self?.animateUp()
  66.             return event
  67.         }
  68.     }
  69.    
  70.     func updateCursorPosition() {
  71.         let mouseLoc = NSEvent.mouseLocation
  72.         DispatchQueue.main.async {
  73.             // 减去 rectSize/2 (也就是 20 像素),确保物理鼠标的那个“尖尖”死死顶在圆圈的正中央
  74.             self.cursorWindow.setFrameOrigin(NSPoint(x: mouseLoc.x - self.rectSize/2, y: mouseLoc.y - self.rectSize/2))
  75.         }
  76.     }
  77.    
  78.     func animateDown() {
  79.         DispatchQueue.main.async {
  80.             CATransaction.begin()
  81.             CATransaction.setAnimationDuration(0.10) // 极致流畅的 100ms 快速下蹲
  82.             self.cursorLayer.transform = CATransform3DMakeScale(0.78, 0.78, 1.0)
  83.             self.cursorLayer.backgroundColor = NSColor(white: 0.22, alpha: 0.70).cgColor // 点击时圆圈质感变深沉
  84.             CATransaction.commit()
  85.         }
  86.     }
  87.    
  88.     func animateUp() {
  89.         DispatchQueue.main.async {
  90.             CATransaction.begin()
  91.             CATransaction.setAnimationDuration(0.16) // Q 弹回弹
  92.             self.cursorLayer.transform = CATransform3DIdentity
  93.             self.cursorLayer.backgroundColor = NSColor(white: 0.31, alpha: 0.45).cgColor // 恢复原状
  94.             CATransaction.commit()
  95.         }
  96.     }
  97. }

  98. let app = NSApplication.shared
  99. app.setActivationPolicy(.prohibited)
  100. let overlay = CursorOverlay()
  101. app.run()
复制代码
  1. swiftc main.swift -o TouchCastVisual && ./TouchCastVisual
复制代码
本来想贴在 Swift开发 版块的,进不了,就贴这里吧。




发表于 昨天 23:19 | 显示全部楼层
牛哈 加油
回复

使用道具 举报

发表于 昨天 23:55 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|捐助支持|无忧启动 ( 闽ICP备05002490号-1|闽公网安备35020302032614号 )

GMT+8, 2026-5-21 01:48

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表