GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - robb/AtRandom: @​Random – Stable, random values for SwiftUI

BranchesTags
Open more actions menu

Latest commit

 

History

7 Commits
7 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
Sources/AtRandom
Sources/AtRandom
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
Package.swift
Package.swift
 
 
README.md
README.md
 
 

Repository files navigation

@Random

A property wrapper that assigns a stable random value to a member variable, persisting across multiple body invocations.

Use this to introduce variation in animations without relying on @State.

For example

struct RandomOffsetTransition: Transition {
    @Random(distance: 20) var offset: CGSize

    func body(content: Content, phase: TransitionPhase) -> some View {
        content.offset(phase.isIdentity ? .zero : offset)
    }
}

will create a transition that animates the view from a random offset.

Fixed Seeds

By default, different instances of the same View or ViewModifier generate unique @Random values. To ensure consistency, you can provide a custom seed via the property wrapper’s projectedValue's seed value.

For example, assigning a model’s hashValue to $greeting.seed ensures stability when the view gets recreated, say in a LazyVStack.

struct Cell: View {
    @Random(in: "Hello", "Bonjour", "Willkommen") var greeting

    var model: Model

    init(model: Model) {
        self.model = model
        $greeting.seed = model.hashValue
    }

    var body: some View {
      // The same `model` will get the same `greeting`, every time.
      Text("\(greeting), \(model.name)!")
    }
}

This way, each Cell instance with the same model will always produce the same greeting.

Important

Some Swift types, like String, generate different hashValues across app launches. For values that persist across launches, use a stable identifier, such as a user ID.

About

@​Random – Stable, random values for SwiftUI

Topics

animationspcg-randompolishswiftuiviewmodifier

Resources

Readme
MIT license
Activity

Stars

64 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Sponsor this project

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.