Last active
April 3, 2020 14:39
-
-
Save dfrobison/6f43fb4ad1e875a227f168e6518b9168 to your computer and use it in GitHub Desktop.
[Wobbing Cone] from https://www.reddit.com/r/SwiftUI/comments/fsj0yp/neat_wobbling_tower_effect_code_in_comments/
This file contains 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
extension CGSize { | |
public static func + (lhs: CGSize, rhs: CGSize) -> CGSize { | |
CGSize(width: lhs.width+rhs.width, | |
height: lhs.height+rhs.height) | |
} | |
public static func += (lhs: inout CGSize, rhs: CGSize) { | |
lhs.width += rhs.width | |
lhs.height += rhs.height | |
} | |
} | |
struct WobblyCone: View { | |
@State private var offset: CGSize = .zero | |
@State private var dragState: CGSize = .zero | |
private let hueColors: [Double] = stride(from: 0, to: 1, by: 0.04).map({$0}) | |
var body: some View { | |
let gesture = DragGesture() | |
.onChanged({ self.dragState = $0.translation }) | |
.onEnded({ (value) in | |
self.offset += value.translation | |
self.dragState = .zero | |
}) | |
return ZStack { | |
ForEach(hueColors, id: \.self) { (hue) in | |
Circle() | |
.fill(Color(hue: hue, saturation: 1, brightness: 1)) | |
.padding() | |
.scaleEffect(CGFloat(1.0-hue)) | |
.offset(self.offset + self.dragState) | |
.gesture(gesture) | |
.animation(.easeInOut(duration: 1 + 2*hue)) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment