|
|
本帖最后由 seekyou2008 于 2026-5-20 22:28 编辑
之前剪demo发现鼠标效果太差了,于是用swift手搓了一个,参考了chrome模拟移动端界面里鼠标效果。整体看起来灰色加透明,还有一点放大镜的感觉。效果如下:

代码如下:
- import Cocoa
- import Foundation
- import QuartzCore
- class CursorOverlay: NSObject {
- var cursorWindow: NSWindow!
- var cursorLayer: CALayer!
- let rectSize: CGFloat = 40.0 // 40x40 像素,大小非常克制、高级
-
- override init() {
- super.init()
- setupCursorWindow()
- setupEventMonitors()
- }
-
- func setupCursorWindow() {
- let rect = NSRect(x: 0, y: 0, width: rectSize, height: rectSize)
-
- cursorWindow = NSWindow(contentRect: rect,
- styleMask: .borderless,
- backing: .buffered,
- defer: false)
- cursorWindow.isOpaque = false
- cursorWindow.backgroundColor = .clear
- cursorWindow.level = .screenSaver
- cursorWindow.ignoresMouseEvents = true
- cursorWindow.hasShadow = false
-
- let view = NSView(frame: rect)
- view.wantsLayer = true
-
- // 绘制完全体深空灰半透明圆圈
- cursorLayer = CALayer()
- cursorLayer.frame = rect
- cursorLayer.anchorPoint = CGPoint(x: 0.5, y: 0.5) // 强制锁死动画锚点在正中心
-
- // 颜色调教:比普通灰色更高级的深空灰 (rgba(80, 80, 80, 0.45))
- cursorLayer.backgroundColor = NSColor(white: 0.31, alpha: 0.45).cgColor
- cursorLayer.cornerRadius = rectSize / 2
-
- // ✨ 新增:高光防爆边框 (1.5 像素宽,稍微亮一点的半透明灰)
- cursorLayer.borderWidth = 1.5
- cursorLayer.borderColor = NSColor(white: 0.5, alpha: 0.4).cgColor
-
- view.layer?.addSublayer(cursorLayer)
- cursorWindow.contentView = view
- cursorWindow.makeKeyAndOrderFront(nil)
- updateCursorPosition()
- }
-
- func setupEventMonitors() {
- NSEvent.addGlobalMonitorForEvents(matching: [.mouseMoved, .leftMouseDragged]) { [weak self] _ in self?.updateCursorPosition() }
- NSEvent.addLocalMonitorForEvents(matching: [.mouseMoved, .leftMouseDragged]) { [weak self] event in
- self?.updateCursorPosition()
- return event
- }
-
- NSEvent.addGlobalMonitorForEvents(matching: [.leftMouseDown]) { [weak self] _ in self?.animateDown() }
- NSEvent.addLocalMonitorForEvents(matching: [.leftMouseDown]) { [weak self] event in
- self?.animateDown()
- return event
- }
-
- NSEvent.addGlobalMonitorForEvents(matching: [.leftMouseUp]) { [weak self] _ in self?.animateUp() }
- NSEvent.addLocalMonitorForEvents(matching: [.leftMouseUp]) { [weak self] event in
- self?.animateUp()
- return event
- }
- }
-
- func updateCursorPosition() {
- let mouseLoc = NSEvent.mouseLocation
- DispatchQueue.main.async {
- // 减去 rectSize/2 (也就是 20 像素),确保物理鼠标的那个“尖尖”死死顶在圆圈的正中央
- self.cursorWindow.setFrameOrigin(NSPoint(x: mouseLoc.x - self.rectSize/2, y: mouseLoc.y - self.rectSize/2))
- }
- }
-
- func animateDown() {
- DispatchQueue.main.async {
- CATransaction.begin()
- CATransaction.setAnimationDuration(0.10) // 极致流畅的 100ms 快速下蹲
- self.cursorLayer.transform = CATransform3DMakeScale(0.78, 0.78, 1.0)
- self.cursorLayer.backgroundColor = NSColor(white: 0.22, alpha: 0.70).cgColor // 点击时圆圈质感变深沉
- CATransaction.commit()
- }
- }
-
- func animateUp() {
- DispatchQueue.main.async {
- CATransaction.begin()
- CATransaction.setAnimationDuration(0.16) // Q 弹回弹
- self.cursorLayer.transform = CATransform3DIdentity
- self.cursorLayer.backgroundColor = NSColor(white: 0.31, alpha: 0.45).cgColor // 恢复原状
- CATransaction.commit()
- }
- }
- }
- let app = NSApplication.shared
- app.setActivationPolicy(.prohibited)
- let overlay = CursorOverlay()
- app.run()
复制代码- swiftc main.swift -o TouchCastVisual && ./TouchCastVisual
复制代码 本来想贴在 Swift开发 版块的,进不了,就贴这里吧。
|
|