Skip to content

Instantly share code, notes, and snippets.

@CodeSlicing
Created April 17, 2021 10:04
Show Gist options
  • Save CodeSlicing/36b036ba7fa4022e0ce25105df7eb820 to your computer and use it in GitHub Desktop.
Save CodeSlicing/36b036ba7fa4022e0ce25105df7eb820 to your computer and use it in GitHub Desktop.
Source code for CodeSlicing episode on Animated Weather Icons Part 2 - Creating the Sun (requires PureSwiftUI v2.1.0+)
//
// WeatherIconSunDemo.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 18/04/2021.
// Copyright © 2021 Adam Fordyce. All rights reserved.
//
import SwiftUI
import PureSwiftUI
private struct SunElement: View {
let showingRays: Bool
var body: some View {
SunView(showingRays: showingRays)
.scale(0.7)
}
}
private struct SunView: View {
let showingRays: Bool
@State private var rotatingRays = false
var body: some View {
GeometryReader { (geo: GeometryProxy) in
ZStack {
ForEach(0..<8) { rayIndex in
Capsule()
.fillColor(.yellow)
.frame(geo.sizeScaled(0.08, 0.2))
.yOffsetIf(showingRays, geo.widthScaled(-0.4))
.rotate(45.degrees * rayIndex)
}
.rotateIf(rotatingRays, 360.degrees)
Circle()
.fillColor(.yellow)
.frame(geo.widthScaled(0.45))
}
.greedyFrame()
}
.onAppear {
withAnimation(Animation.linear(duration: 8).repeatForever(autoreverses: false)) {
rotatingRays = true
}
}
}
}
struct WeatherIconSunDemo_Previews: PreviewProvider {
struct WeatherIconSunDemo_Harness: View {
@State private var showingRays = false
var body: some View {
SunElement(showingRays: showingRays)
.frame(400)
.onAppear {
withAnimation {
showingRays = true
}
}
}
}
static var previews: some View {
WeatherIconSunDemo_Harness()
.padding(50)
.previewSizeThatFits()
.previewDevice(.iPhone_12_Pro_Max)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment