### macOS SpringMotionView Setup and Animation Source: https://context7.com/macpaw/cocoasprings/llms.txt Sets up SpringMotionView on macOS with custom spring physics and an `onPositionUpdate` closure for constraint adjustments. Responds to mouse down events to animate the view to the clicked position. ```swift import AppKit import CocoaSprings final class MacViewController: NSViewController { @IBOutlet weak var springMotionView: SpringMotionView! @IBOutlet weak var springMotionViewTopConstraint: NSLayoutConstraint! @IBOutlet weak var springMotionViewLeftConstraint: NSLayoutConstraint! override func viewDidLoad() { super.viewDidLoad() // Configure with custom spring physics springMotionView.configuration = SpringConfiguration(angularFrequency: 7.5, dampingRatio: 0.6) // Set up the position update callback for constraint-based layout springMotionView.onPositionUpdate = { [weak self] point in guard let self else { return } let size = self.springMotionView.frame.size self.springMotionViewLeftConstraint.constant = point.x - size.width / 2 self.springMotionViewTopConstraint.constant = point.y - size.height / 2 } } override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) // Move to clicked position let locationInView = view.convert(event.locationInWindow, from: nil) springMotionView.move(to: locationInView) } } ``` -------------------------------- ### iOS SpringMotionView Setup and Animation Source: https://context7.com/macpaw/cocoasprings/llms.txt Configures SpringMotionView with custom physics and sets up an `onPositionUpdate` closure to modify Auto Layout constraints for positioning. Handles tap gestures to animate the view to a new location. ```swift import UIKit import CocoaSprings final class ViewController: UIViewController { @IBOutlet weak var springMotionView: SpringMotionView! @IBOutlet weak var springMotionViewTopConstraint: NSLayoutConstraint! @IBOutlet weak var springMotionViewLeftConstraint: NSLayoutConstraint! override func viewDidLoad() { super.viewDidLoad() // Configure spring physics for the view springMotionView.configuration = SpringConfiguration(angularFrequency: 8.0, dampingRatio: 0.5) // Set up position update handler to modify constraints // This closure is called on every frame during animation springMotionView.onPositionUpdate = { [weak self] point in guard let self else { return } let size = self.springMotionView.frame.size // Update constraints to position view centered at the given point self.springMotionViewLeftConstraint.constant = point.x - size.width / 2 self.springMotionViewTopConstraint.constant = point.y - size.height / 2 } } // UITapGestureRecognizer action connected in Interface Builder @IBAction func handleTap(_ sender: UITapGestureRecognizer) { // Start spring animation to the tapped location // The point represents the view's target center position springMotionView.move(to: sender.location(in: view)) } // Programmatic example func moveViewToCenter() { let centerPoint = CGPoint(x: view.bounds.midX, y: view.bounds.midY) springMotionView.move(to: centerPoint) } } ``` -------------------------------- ### CocoaSprings Swift Package Manager Installation Source: https://context7.com/macpaw/cocoasprings/llms.txt Instructions for adding the CocoaSprings library as a dependency using Swift Package Manager in your Xcode project or `Package.swift` file. ```swift // Package.swift dependencies: [ .package(url: "https://github.com/MacPaw/CocoaSprings.git", branch: "main") ] // Add to your target dependencies .target( name: "YourTarget", dependencies: ["CocoaSprings"] ) ``` -------------------------------- ### Animate SpringMotionLayer on Click Source: https://github.com/macpaw/cocoasprings/blob/main/README.md This example demonstrates a custom NSView subclass that hosts a SpringMotionLayer. It animates the layer's position to the clicked point within the view using the move(to:) method. ```swift import AppKit import CocoaSprings final class ClickableView: NSView { override init(frame frameRect: NSRect) { super.init(frame: frameRect) setup() } required init?(coder: NSCoder) { super.init(coder: coder) setup() } private lazy var rootLayer = CALayer() private lazy var springMotionLayer: SpringMotionLayer = { let layer = SpringMotionLayer() layer.backgroundColor = NSColor.systemRed.cgColor layer.cornerRadius = 10 layer.frame.size = .init(width: 20, height: 20) return layer }() private func setup() { layer = rootLayer wantsLayer = true rootLayer.addSublayer(springMotionLayer) } override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) springMotionLayer.move(to: convert(event.locationInWindow, from: nil)) } } ``` -------------------------------- ### Create and Configure SpringMotionWindow Source: https://context7.com/macpaw/cocoasprings/llms.txt Demonstrates creating a `SpringMotionWindow`, configuring its appearance, setting spring physics, and pinning it to follow the main application window with an offset. ```swift import AppKit import CocoaSprings final class ViewController: NSViewController { private var springMotionWindow: SpringMotionWindow? override func viewDidAppear() { super.viewDidAppear() // Create a spring motion window let floatingWindow = SpringMotionWindow() floatingWindow.contentView = NSView() floatingWindow.contentView?.wantsLayer = true floatingWindow.contentView?.layer?.backgroundColor = NSColor.systemRed.cgColor floatingWindow.contentView?.layer?.cornerRadius = 10 // Configure window appearance floatingWindow.setFrame(.init(origin: .zero, size: .init(width: 100, height: 100)), display: true) floatingWindow.makeKeyAndOrderFront(nil) floatingWindow.level = .mainMenu floatingWindow.styleMask = [.borderless, .fullSizeContentView] floatingWindow.backgroundColor = .clear floatingWindow.isMovableByWindowBackground = true // Configure spring physics floatingWindow.configuration = SpringConfiguration(angularFrequency: 8.0, dampingRatio: 0.5) // Pin to follow the main window with offset // The floating window will follow mainWindow's movements with spring animation if let mainWindow = view.window { floatingWindow.pinToWindow(mainWindow, offsetFromCenter: .init(x: 300, y: 100)) } springMotionWindow = floatingWindow } // Move window to specific screen position func moveWindowToPosition(_ point: NSPoint) { springMotionWindow?.move(to: point) } // Stop following the main window func detachFloatingWindow() { springMotionWindow?.unpinFromWindow() } // Re-pin to a different window func attachToWindow(_ targetWindow: NSWindow) { springMotionWindow?.pinToWindow(targetWindow, offsetFromCenter: .init(x: 200, y: 0)) } } ``` -------------------------------- ### Create and Configure SpringMotionPanel Source: https://context7.com/macpaw/cocoasprings/llms.txt Shows how to instantiate a `SpringMotionPanel`, customize its appearance and behavior, set a snappier spring configuration, and pin it to follow an anchor window. ```swift import AppKit import CocoaSprings final class PanelController: NSViewController { private var utilityPanel: SpringMotionPanel? func showUtilityPanel(anchoredTo window: NSWindow) { // Create a utility panel with spring motion let panel = SpringMotionPanel() panel.contentView = NSView() panel.contentView?.wantsLayer = true panel.contentView?.layer?.backgroundColor = NSColor.systemBlue.cgColor panel.contentView?.layer?.cornerRadius = 8 // Configure panel properties panel.setFrame(.init(origin: .zero, size: .init(width: 150, height: 80)), display: true) panel.level = .floating panel.styleMask = [.borderless, .nonactivatingPanel] panel.backgroundColor = .clear panel.isMovableByWindowBackground = true panel.hidesOnDeactivate = false // Configure spring physics - snappier response panel.configuration = SpringConfiguration(angularFrequency: 12.0, dampingRatio: 0.7) // Pin panel to follow the anchor window panel.pinToWindow(window, offsetFromCenter: .init(x: 0, y: -150)) panel.orderFront(nil) utilityPanel = panel } func movePanel(to screenPoint: NSPoint) { // Detach from anchor window and move to specific position utilityPanel?.unpinFromWindow() utilityPanel?.move(to: screenPoint) } func dismissPanel() { utilityPanel?.unpinFromWindow() utilityPanel?.close() utilityPanel = nil } } ``` -------------------------------- ### Configure Spring Physics with SpringConfiguration Source: https://context7.com/macpaw/cocoasprings/llms.txt Use `SpringConfiguration` to define animation speed and oscillation decay. Default values are provided, and custom configurations can be applied to various spring motion components. ```swift import CocoaSprings // Use default configuration (angularFrequency: 7.5, dampingRatio: 0.5) let defaultConfig = SpringConfiguration.default // Create custom configuration for faster, bouncier animation let bouncyConfig = SpringConfiguration(angularFrequency: 10.0, dampingRatio: 0.3) // Create custom configuration for slower, more damped animation let smoothConfig = SpringConfiguration(angularFrequency: 5.0, dampingRatio: 0.8) // Apply configuration to any spring motion component let layer = SpringMotionLayer() layer.configuration = bouncyConfig let view = SpringMotionView() view.configuration = smoothConfig let window = SpringMotionWindow() window.configuration = SpringConfiguration(angularFrequency: 12.0, dampingRatio: 0.6) ``` -------------------------------- ### Configure Spring Physics Source: https://github.com/macpaw/cocoasprings/blob/main/README.md Adjust the animation physics by setting the angularFrequency and dampingRatio on a SpringConfiguration object. Higher angularFrequency increases speed, while dampingRatio controls oscillation decay. ```swift let layer = SpringMotionLayer() layer.configuration = SpringConfiguration(angularFrequency: 10, dampingRatio: 0.7) ``` -------------------------------- ### Pin SpringMotionWindow to Another Window on macOS Source: https://github.com/macpaw/cocoasprings/blob/main/README.md Use pinToWindow to make a SpringMotionWindow follow another window with spring animation. Ensure the target window is accessible. ```swift import AppKit import CocoaSprings final class ViewController: NSViewController { override func viewDidAppear() { super.viewDidAppear() let springMotionWindow = SpringMotionWindow() springMotionWindow.contentView = NSView() springMotionWindow.contentView?.wantsLayer = true springMotionWindow.contentView?.layer?.backgroundColor = NSColor.systemRed.cgColor springMotionWindow.contentView?.layer?.cornerRadius = 10 springMotionWindow.setFrame(.init(origin: .zero, size: .init(width: 100, height: 100)), display: true) springMotionWindow.makeKeyAndOrderFront(nil) springMotionWindow.level = .mainMenu springMotionWindow.styleMask = [.borderless, .fullSizeContentView] springMotionWindow.backgroundColor = .clear springMotionWindow.isMovableByWindowBackground = true if let mainWindow = view.window { springMotionWindow.pinToWindow(mainWindow, offsetFromCenter: .init(x: 300, y: 100)) } } } ``` -------------------------------- ### Animate CALayer Position with SpringMotionLayer Source: https://context7.com/macpaw/cocoasprings/llms.txt Subclass `CALayer` with `SpringMotionLayer` to animate layer positions using spring physics. The layer handles animation interruptions and redirects smoothly. Ensure the layer is added as a sublayer and its `configuration` is set. ```swift import AppKit // or UIKit for iOS import CocoaSprings final class ClickableView: NSView { override init(frame frameRect: NSRect) { super.init(frame: frameRect) setup() } required init?(coder: NSCoder) { super.init(coder: coder) setup() } private lazy var rootLayer = CALayer() private lazy var springMotionLayer: SpringMotionLayer = { let layer = SpringMotionLayer() layer.backgroundColor = NSColor.systemRed.cgColor layer.cornerRadius = 10 layer.frame.size = .init(width: 20, height: 20) // Configure spring physics layer.configuration = SpringConfiguration(angularFrequency: 10, dampingRatio: 0.7) return layer }() private func setup() { layer = rootLayer wantsLayer = true rootLayer.addSublayer(springMotionLayer) } override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) // Move layer to clicked position with spring animation // The point represents the layer's center position springMotionLayer.move(to: convert(event.locationInWindow, from: nil)) } } ``` -------------------------------- ### Add CocoaSprings Dependency Source: https://github.com/macpaw/cocoasprings/blob/main/README.md Add the CocoaSprings repository as a dependency to your Swift Package Manager project. ```swift dependencies: [ .package(url: "https://github.com/MacPaw/CocoaSprings.git", branch: "main") ] ``` -------------------------------- ### Update SpringMotionView Constraints on iOS Source: https://github.com/macpaw/cocoasprings/blob/main/README.md Use the onPositionUpdate closure to update view constraints when the SpringMotionView moves. This is crucial for integrating with constraint-based layouts. ```swift import UIKit import CocoaSprings final class ViewController: UIViewController { @IBOutlet weak var springMotionView: SpringMotionView! @IBOutlet weak var springMotionViewTopConstraint: NSLayoutConstraint! @IBOutlet weak var springMotionViewLeftConstraint: NSLayoutConstraint! override func viewDidLoad() { super.viewDidLoad() // Here we set up how the view should update its position // depending on the provided `point` parameter. springMotionView.onPositionUpdate = { [weak self] point in guard let self else { return } let size = self.springMotionView.frame.size self.springMotionViewLeftConstraint.constant = point.x - size.width / 2 self.springMotionViewTopConstraint.constant = point.y - size.height / 2 } } // UITapGestureRecognizer action @IBAction func handleTap(_ sender: UITapGestureRecognizer) { // Start moving to the tapped location springMotionView.move(to: sender.location(in: view)) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.