Skip to content

Instantly share code, notes, and snippets.

@CodeSlicing
Created April 17, 2021 10:02
Show Gist options
  • Save CodeSlicing/94ecfe068149ea16abb1593fb55da72a to your computer and use it in GitHub Desktop.
Save CodeSlicing/94ecfe068149ea16abb1593fb55da72a to your computer and use it in GitHub Desktop.
Native Source code for CodeSlicing episode on Animated Weather Icons Part 2 - Creating the Sun
//
// WeatherIconSunDemoNative.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
private struct SunElement: View {
let showingRays: Bool
var body: some View {
SunView(showingRays: showingRays)
.scaleEffect(0.7)
}
}
private struct SunView: View {
let showingRays: Bool
@State private var rotatingRays = false
var body: some View {
GeometryReader { (geo: GeometryProxy) in
let rayWidth = geo.size.width * 0.08
let rayHeight = geo.size.height * 0.2
let rayOffset = geo.size.width * -0.4
let sunDiameter = geo.size.width * 0.45
ZStack {
ForEach(0..<8) { rayIndex in
Capsule()
.fill(Color.yellow)
.frame(width: rayWidth, height: rayHeight)
.offset(y: showingRays ? rayOffset : 0)
.rotationEffect(.degrees(Double(45 * rayIndex)))
}
.rotationEffect(.degrees(rotatingRays ? 360 : 0))
Circle()
.fill(Color.yellow)
.frame(width: sunDiameter, height: sunDiameter)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.onAppear {
withAnimation(Animation.linear(duration: 8).repeatForever(autoreverses: false)) {
rotatingRays = true
}
}
}
}
struct WeatherIconSunDemoNative_Previews: PreviewProvider {
struct WeatherIconSunDemoNative_Harness: View {
@State private var showingRays = false
var body: some View {
ZStack {
// Image(systemName: "sun.max.fill")
// .resizable()
// .aspectRatio(contentMode: .fit)
// .foregroundColor(.black.opacity(0.2))
SunElement(showingRays: showingRays)
}
.frame(width: 400, height: 400)
.onAppear {
withAnimation {
showingRays = true
}
}
}
}
static var previews: some View {
WeatherIconSunDemoNative_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