Last active
April 28, 2021 22:07
-
-
Save CodeSlicing/6c0bba571b37cd9d63030e45ff30b1f3 to your computer and use it in GitHub Desktop.
Native Source code for CodeSlicing episode on Animated Pac-Man (requires PureSwiftUI v2.1.0+)
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
// | |
// AnimatedPacManDemoNative.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 23/04/2021. | |
// Copyright © 2020 Adam Fordyce. All rights reserved. | |
// | |
import SwiftUI | |
private let pacManDiameter: CGFloat = 100 | |
private let maxOffset = UIScreen.main.bounds.width * 0.5 + pacManDiameter * 0.5 | |
struct AnimatedPacManDemoNative: View { | |
@State private var animatingMouth = false | |
@State private var animatingPosition = false | |
var body: some View { | |
let xOffset = maxOffset * (animatingPosition ? 1 : -1) | |
PacMan(animatingMouth: animatingMouth) | |
.fill(Color.yellow, style: .init(eoFill: true)) | |
.frame(width: pacManDiameter, height: pacManDiameter) | |
.offset(x: xOffset) | |
.onAppear { | |
withAnimation(Animation.easeInOut(duration: 0.2).repeatForever()) { | |
animatingMouth = true | |
} | |
withAnimation(Animation.linear(duration: 3).repeatForever(autoreverses: false)) { | |
animatingPosition = true | |
} | |
} | |
} | |
} | |
private struct PacMan: Shape { | |
var animatableData: CGFloat | |
init(animatingMouth: Bool) { | |
animatableData = animatingMouth ? 1 : 0 | |
} | |
func path(in rect: CGRect) -> Path { | |
var path = Path() | |
// mouth | |
let rectCenter = CGPoint(x: rect.midX, y: rect.midY) | |
path.move(to: rectCenter) | |
let startAngleMax = Angle.degrees(0) | |
let startAngleMin = Angle.degrees(45) | |
let startAngleDeltaDegrees = startAngleMax.degrees - startAngleMin.degrees | |
let startAngleDegrees = startAngleMin.degrees + (startAngleDeltaDegrees * Double(animatableData)) | |
let endAngleMax = Angle.degrees(-0.001) | |
let endAngleMin = Angle.degrees(-45) | |
let endAngleDeltaDegrees = endAngleMax.degrees - endAngleMin.degrees | |
let endAngleDegrees = endAngleMin.degrees + (endAngleDeltaDegrees * Double(animatableData)) | |
let startAngle = Angle.degrees(startAngleDegrees) | |
let endAngle = Angle.degrees(endAngleDegrees) | |
path.addArc(center: rectCenter, radius: rect.width * 0.5, startAngle: startAngle, endAngle: endAngle, clockwise: false) | |
// eye | |
let eyePosition = CGPoint(x: rect.minX + rect.width * 0.65, | |
y: rect.minY + rect.height * 0.2) | |
let eyeRadius = rect.width * 0.05 | |
let eyeDiameter = eyeRadius * 2 | |
let eyeOrigin = CGPoint(x: eyePosition.x - eyeRadius, y: eyePosition.y - eyeRadius) | |
let eyeRect = CGRect(origin: eyeOrigin, size: CGSize(width: eyeDiameter, height: eyeDiameter)) | |
path.addEllipse(in: eyeRect) | |
return path | |
} | |
} | |
struct AnimatedPacManDemoNative_Previews: PreviewProvider { | |
struct AnimatedPacManDemoNative_Harness: View { | |
var body: some View { | |
AnimatedPacManDemoNative() | |
.frame(maxWidth: .infinity, maxHeight: .infinity) | |
.foregroundColor(.white) | |
.background(Color(white: 0.1)) | |
.ignoresSafeArea() | |
} | |
} | |
static var previews: some View { | |
AnimatedPacManDemoNative_Harness() | |
.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