Skip to content

Instantly share code, notes, and snippets.

@CodeSlicing
Last active April 28, 2021 22:07
Show Gist options
  • Save CodeSlicing/c38286f3452f62ce7d7f80c629276a27 to your computer and use it in GitHub Desktop.
Save CodeSlicing/c38286f3452f62ce7d7f80c629276a27 to your computer and use it in GitHub Desktop.
Source code for CodeSlicing episode on Animated Pac-Man (requires PureSwiftUI v2.1.0+)
//
// AnimatedPacManDemo.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
import PureSwiftUI
private let pacManSize: CGFloat = 100
private let maxXOffset = UIScreen.halfMainWidth + pacManSize * 0.5
struct AnimatedPacManDemo: View {
@State private var animatingMouth = false
@State private var animatingPosition = false
var body: some View {
let xOffset = maxXOffset * (animatingPosition ? 1 : -1)
PacMan(animatingMouth: animatingMouth)
.eoFill(Color.yellow)
.frame(pacManSize)
.xOffset(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()
path.move(rect.center)
//mouth
path.arc(rect.center, radius: rect.halfHeight,
startAngle: 135.degrees.to(90.degrees, animatableData),
endAngle: 45.degrees.to(89.999.degrees, animatableData))
//eye
path.circle(rect[0.65, 0.2], radius: rect.widthScaled(0.05))
return path
}
}
struct AnimatedPacManDemo_Previews: PreviewProvider {
struct AnimatedPacManDemo_Harness: View {
var body: some View {
AnimatedPacManDemo()
.greedyFrame()
.foregroundColor(.white)
.backgroundColor(.white(0.1))
.ignoresSafeArea()
}
}
static var previews: some View {
AnimatedPacManDemo_Harness()
.previewDevice(.iPhone_12_Pro_Max)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment