Skip to content

Instantly share code, notes, and snippets.

struct ContentView: View {
var body: some View {
/// add the image
Image("background0")
/// we make it fit the screen
.resizable()
.aspectRatio(contentMode: .fit)
}
struct ContentView: View {
/// The timer, which will update the background image.
let timer = Timer.publish(every: 0.04, on: .main, in: .common).autoconnect()
/// so we can keep track of the current image.
/// the images are named:
/// background<number>
/// where number is from 0...21 (check out the Assets folder, 22 images in total)
@State var currentImageNumber = 0
struct ContentView: View {
/// true = green, false = blue
/// because this is a `@State` property, any view (like the rectangle) that accesses it will update whenever this value changes, always keeping in sync
@State var rectangleIsGreen = false
var body: some View {
VStack {
RoundedRectangle(cornerRadius: 16)
/// ternary operator: if `rectangleIsGreen` is true, the fill color should be green
# define the function
def plural_check(string: str, count: int) -> str:
# see if the count is plural
if count != 1:
# first check if it ends in "y"
if string.endswith("y"):
# if the second to last letter is a vowel, just add an "s"
struct ColorView: View {
@Binding var colorRectangleIsGreen: Bool
var body: some View {
RoundedRectangle(cornerRadius: 5)
.fill(colorRectangleIsGreen ? Color.green : Color.blue)
.frame(width: 100, height: 100)
.padding()
}
}
struct ContentView: View {
@State var rectangleIsGreen = false
var body: some View {
HStack {
RoundedRectangle(cornerRadius: 16)
.fill(rectangleIsGreen ? Color.green : Color.blue)
.frame(width: 250, height: 250)
.padding()
RoundedRectangle(cornerRadius: 16)
struct ColorView: View {
@Binding var colorRectangleIsGreen: Bool
var body: some View {
RoundedRectangle(cornerRadius: 16)
.fill(colorRectangleIsGreen ? Color.green : Color.blue)
.frame(width: 250, height: 250)
.padding()
}
}
struct ContentView: View {
struct ColorView: View {
@Binding var colorRectangleIsGreen: Bool
var body: some View {
RoundedRectangle(cornerRadius: 16)
.fill(colorRectangleIsGreen ? Color.green : Color.blue)
.frame(width: 250, height: 250)
.padding()
}
}
import AVFoundation
import SwiftUI
import PlaygroundSupport
struct SwiftUIAudioPlayerView: View {
/// the player that will play the audio. It can't be a local variable.
/// Must have the `@State` attribute, because we need modify it later on
@State var audioPlayer: AVAudioPlayer?
import UIKit
import SwiftUI
// Auto-generated code
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {