Last active
May 8, 2021 08:26
-
-
Save CodeSlicing/367f19c2ff328eba747064d84f244731 to your computer and use it in GitHub Desktop.
Native Source code for CodeSlicing episode on Animated Weather Icons Part 3 - Making it Rain
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
// | |
// WeatherIconRainDemoNative.swift | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
// of the Software, and to permit persons to whom the Software is furnished to do so, | |
// subject to the following conditions: | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | |
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
// | |
// Created by Adam Fordyce on 28/04/2021. | |
// Copyright © 2021 Adam Fordyce. All rights reserved. | |
// | |
import SwiftUI | |
private struct RainElement: View { | |
var body: some View { | |
RainView() | |
} | |
} | |
private struct RainView: View { | |
@State private var animating = false | |
var body: some View { | |
GeometryReader { (geo: GeometryProxy) in | |
RainShape(animatableData: animating ? 1 : 0) | |
.stroke(Color.blue, style: .init(lineWidth: geo.size.width * 0.07, lineCap: .round)) | |
} | |
.onAppear { | |
withAnimation(Animation.linear(duration: 1).repeatForever(autoreverses: false)) { | |
animating = true | |
} | |
} | |
} | |
} | |
private struct RainShape: Shape { | |
var animatableData: CGFloat | |
func path(in rect: CGRect) -> Path { | |
var path = Path() | |
let yOffset = rect.height * 0.35 | |
let animationOffset = yOffset * animatableData | |
let col1X = rect.minX + rect.width * 0.3 | |
let col2X = rect.minX + rect.width * 0.5 | |
let col3X = rect.minX + rect.width * 0.7 | |
for row in 0...2 { | |
let rowYOffset = CGFloat(row) * yOffset + animationOffset | |
let drop1Start = CGPoint(x: col1X, y: rect.minY + rect.height * 0.12 + rowYOffset) | |
let drop1End = CGPoint(x: col1X, y: rect.minY + rect.height * 0.29 + rowYOffset) | |
path.move(to: drop1Start) | |
path.addLine(to: drop1End) | |
let drop2Start = CGPoint(x: col2X, y: rect.minY + rect.height * 0 + rowYOffset) | |
let drop2End = CGPoint(x: col2X, y: rect.minY + rect.height * 0.15 + rowYOffset) | |
path.move(to: drop2Start) | |
path.addLine(to: drop2End) | |
let drop3Start = CGPoint(x: col3X, y: rect.minY + rect.height * 0.05 + rowYOffset) | |
let drop3End = CGPoint(x: col3X, y: rect.minY + rect.height * 0.2 + rowYOffset) | |
path.move(to: drop3Start) | |
path.addLine(to: drop3End) | |
} | |
return path | |
} | |
} | |
struct Rain_Previews: PreviewProvider { | |
struct Rain_Harness: View { | |
var body: some View { | |
RainElement() | |
.frame(width: 400, height: 400) | |
} | |
} | |
static var previews: some View { | |
Rain_Harness() | |
.padding(50) | |
.previewLayout(.sizeThatFits) | |
.previewDevice("iPhone 12 Pro Max") | |
.previewDisplayName("iPhone 12 Pro Max") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment