-
-
Save Pyroh/af66b6c6d4f02f727163c0a1b9c37ecf to your computer and use it in GitHub Desktop.
Trying to replicate an inset scroll view effect in SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
struct HeightPreferenceKey: PreferenceKey { | |
let heigh: Anchor<CGRect>? | |
static func reduce(value: inout Anchor<CGRect>?, nextValue: () -> Anchor<CGRect>?) { | |
value = nextValue() | |
} | |
} | |
@State private var bottomSheetHeight: CGFloat? | |
@ViewBuilder var bottomSheet: some View { | |
VStack(spacing: 20.0) { | |
Text(shorterText) | |
Button(action: { | |
print("*gobble gobble*") | |
}, label: { | |
Text("Gobble the Muffin") | |
.font(.system(size: 19.0, weight: .semibold)) | |
}) | |
} | |
.foregroundColor(.white) | |
.padding() | |
.frame(maxWidth: .infinity) | |
.background(Color.purple.opacity(0.95)) | |
.anchorPreference(key: HeightPreferenceKey.self, value: .bounds, transform: { $0 }) | |
} | |
var body: some View { | |
GeometryReader { p in | |
ScrollView { | |
Text(longText) | |
.padding(.horizontal) | |
.padding(.bottom) | |
.padding(.bottom, bottomSheetHeight ?? 0) | |
} | |
.overlay(bottomSheet, alignment: .bottom) | |
.onPreferenceChange(HeightPreferenceKey.self) { anchor in | |
bottomSheetHeight = anchor.map { p[$0].height } | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
var longText = """ | |
Run in circles head nudges miaow then turn around and show you my bum time to go zooooom and climb leg. Lick arm hair sun bathe, but stretch out on bed or purr like an angel or roll on the floor purring your whiskers off. Snuggles up to shoulders or knees and purrs you to sleep missing until dinner time hunt by meowing loudly at 5am next to human slave food dispenser so fish i must find my red catnip fishy fish. Love and coo around boyfriend who purrs and makes the perfect moonlight eyes so i can purr and swat the glittery gleaming yarn to him (the yarn is from a $125 sweater) sit in window and stare oooh, a bird, yum and cats are fats i like to pets them they like to meow back but chase the pig around the house so at four in the morning wake up owner meeeeeeooww scratch at legs and beg for food then cry and yowl until they wake up at two pm jump on window and sleep while observing the bootyful cat next door that u really like but who already has a boyfriend end up making babies with her and let her move in. I like frogs and 0 gravity claw drapes. Burrow under covers mmmmmmmmmeeeeeeeeooooooooowwwwwwww cat snacks, or stare at the wall, play with food and get confused by dust but claws in the eye of the beholder for meow loudly just to annoy owners so purr while eating. Damn that dog cat walks in keyboard or cough mew i will ruin the couch with my claws or eat all the power cords. Spit up on light gray carpet instead of adjacent linoleum licks paws spend all night ensuring people don't sleep sleep all day small kitty warm kitty little balls of fur stare at ceiling. Immediately regret falling into bathtub push your water glass on the floor decide to want nothing to do with my owner today. Kitty kitty ha ha, you're funny i'll kill you last, so leave dead animals as gifts, but check cat door for ambush 10 times before coming in. Sees bird in air, breaks into cage and attacks creature rub whiskers on bare skin act innocent or always hungry for leave fur on owners clothes i like fish. Unwrap toilet paper check cat door for ambush 10 times before coming in disappear for four days and return home with an expensive injury; bite the vet, or licks your face or do not try to mix old food with new one to fool me!. Catto munch salmono hell is other people so kitten is playing with dead mouse paw at your fat belly or do not try to mix old food with new one to fool me!, walk on keyboard . Scratch the postman wake up lick paw wake up owner meow meow milk the cow. Rub face on owner. Stare at ceiling sleep over your phone and make cute snoring noises so lies down and blow up sofa in 3 seconds. Cats are a queer kind of folk poop on couch shove bum in owner's face like camera lens loved it, hated it, loved it, hated it but licks your face or snuggles up to shoulders or knees and purrs you to sleep or my cat stared at me he was sipping his tea, too. Chase imaginary bugs meow in empty rooms. Cats are the world steal mom's crouton while she is in the bathroom cry louder at reflection prow?? ew dog you drink from the toilet, yum yum warm milk hotter pls, ouch too hot but leave hair everywhere. Vommit food and eat it again check cat door for ambush 10 times before coming in but plan steps for world domination unwrap toilet paper poop on couch. Spot something, big eyes, big eyes, crouch, shake butt, prepare to pounce run around the house at 4 in the morning. Chase ball of string waffles, jump on fridge paw at your fat belly but nya nya nyan. Cat milk copy park pee walk owner escape bored tired cage droppings sick vet vomit blow up sofa in 3 seconds wack the mini furry mouse, meow meow, i tell my human and i show my fluffy belly but it's a trap! if you pet it i will tear up your hand. Instead of drinking water from the cat bowl, make sure to steal water from the toilet so you're just gonna scroll by without saying meowdy?, experiences short bursts of poo-phoria after going to the loo, sweet beast, or eat and than sleep on your face. | |
A nice warm laptop for me to sit on. I dreamt about fish yum! purr as loud as possible, be the most annoying cat that you can, and, knock everything off the table for stuff and things Gate keepers of hell for purr curl into a furry donut howl on top of tall thing. Cough meow to be let in. Pose purrfectly to show my beauty step on your keyboard while you're gaming and then turn in a circle for find empty spot in cupboard and sleep all day sniff catnip and act crazy intently stare at the same spot. Eat and than sleep on your face i bet my nine lives on you-oooo-ooo-hooo and pose purrfectly to show my beauty caticus cuteicus for attack like a vicious monster. Run in circles reaches under door into adjacent room, x shove bum in owner's face like camera lens yet hey! you there, with the hands so allways wanting food. Hate dog. Purr when being pet tweeting a baseball bleghbleghvomit my furball really tie the room together. Brown cats with pink ears purr when being pet but purr when give birth for meow to be let in, meow all night. Paw your face to wake you up in the morning pee in human's bed until he cleans the litter box or pretend you want to go out but then don't a nice warm laptop for me to sit on check cat door for ambush 10 times before coming in yet fall asleep upside-down. Take a big fluffing crap 💩 furrier and even more furrier hairball and attack like a vicious monster, yet groom yourself 4 hours - checked, have your beauty sleep 18 hours - checked, be fabulous for the rest of the day - checked for meow all night having their mate disturbing sleeping humans. Allways wanting food sit on the laptop. | |
""" | |
var shorterText = "I invite you to choose a muffin to eat there a blueberry, raisin, pumpkin, eggplant, mango, apple, bran, and last but not least a banana muffin." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment