用 SpriteKit 创建的快速纸屑视图

ConfettiWithSpriteView

一款由 SpriteKit 创建的 SwiftUI 五彩纸屑修饰符。在 SwiftUI 中易于使用和自定义。

预览

309147241-c4c32903-e5a4-4695-8834-6f5352c29ca6

安装

在 Xcode 中,转到文件 -> Swift 包 -> 添加包依赖关系
并粘贴存储库的 URL:
https://github.com/VIkill33/ConfettiWithSpriteView.git

或者,你可以下载此存储库的代码, 然后在 Xcode 中添加本地..., 并打开存储库文件夹。

用法

  • 安装后,使用 import ConfettiWithSpriteKit 导入此包
  • 如下使用修饰符(在此包中的 DemoAPP 中查看)
struct ContentView: View {
    
    @State private var startConfetti = false
    
    var body: some View {
        ZStack {
            Button("Start/Stop") {
                startConfetti.toggle()
            }
            .confetti(start: $startConfetti)
        }
    }
}

将传入的变量 startConfetti 更改为 true 时,五彩纸屑将在你的修改视图上方播放(五彩纸屑不会处理触碰事件,并将它们传递到下方的视图),而 false 将停止播放五彩纸屑。

需要将变量更改为 false 再更改为 true 以重新触发五彩纸屑。

参数

你可以自定义五彩纸屑的颜色和图。

源代码中的注释中写了有关参数的所有说明:

    /// - Parameters:
    ///   - start: A binding to a boolean value that controls the emission of confetti.
    ///   - angle: The angle at which the confetti will be emitted, measured in radians. Default is pi/2 (90 degrees).
    ///   - colors: An array of `UIColor` values representing the colors of the confetti. Default includes red, green, system teal, yellow, orange, and purple.
    ///   - size: The size of each confetti piece. Default is 25x25 points.
    ///   - scale: The initial scale of the confetti particles. Default is 0.3.
    ///   - numToEmitRange: A closed range of integers specifying the number of particles to emit. Default range is 10 to 20.
    ///   - images: An array of `UIImage` objects representing the images for the confetti. Default includes a ribbon image.

工作原理

它使用 SpriteKit 中的 SKEmitterNode 发射粒子,默认情况下是此包中的彩带。SKEmitterNode 的粒子与 CAEmitterLayer 的行为类似。此存储库也是一个关于如何以编程方式创建 SKEmitterNode 的示例。