www.youtube.com/watch?v=3krC2c56ceQ
Assignment 3.pdf
0.11MB
Reading 3.pdf
0.11MB
Slides.pdf
0.84MB
소스
GridLayout.swfit
//
// GridLayout.swift
// Memorize
//
// Created by CS193p Instructor.
// Copyright © 2020 Stanford University. All rights reserved.
//
import SwiftUI
struct GridLayout {
private(set) var size: CGSize
private(set) var rowCount: Int = 0
private(set) var columnCount: Int = 0
init(itemCount: Int, nearAspectRatio desiredAspectRatio: Double = 1, in size: CGSize) {
self.size = size
// if our size is zero width or height or the itemCount is not > 0
// then we have no work to do (because our rowCount & columnCount will be zero)
guard size.width != 0, size.height != 0, itemCount > 0 else { return }
// find the bestLayout
// i.e., one which results in cells whose aspectRatio
// has the smallestVariance from desiredAspectRatio
// not necessarily most optimal code to do this, but easy to follow (hopefully)
var bestLayout: (rowCount: Int, columnCount: Int) = (1, itemCount)
var smallestVariance: Double?
let sizeAspectRatio = abs(Double(size.width/size.height))
for rows in 1...itemCount {
let columns = (itemCount / rows) + (itemCount % rows > 0 ? 1 : 0)
if (rows - 1) * columns < itemCount {
let itemAspectRatio = sizeAspectRatio * (Double(rows)/Double(columns))
let variance = abs(itemAspectRatio - desiredAspectRatio)
if smallestVariance == nil || variance < smallestVariance! {
smallestVariance = variance
bestLayout = (rowCount: rows, columnCount: columns)
}
}
}
rowCount = bestLayout.rowCount
columnCount = bestLayout.columnCount
}
var itemSize: CGSize {
if rowCount == 0 || columnCount == 0 {
return CGSize.zero
} else {
return CGSize(
width: size.width / CGFloat(columnCount),
height: size.height / CGFloat(rowCount)
)
}
}
func location(ofItemAt index: Int) -> CGPoint {
if rowCount == 0 || columnCount == 0 {
return CGPoint.zero
} else {
return CGPoint(
x: (CGFloat(index % columnCount) + 0.5) * itemSize.width,
y: (CGFloat(index / columnCount) + 0.5) * itemSize.height
)
}
}
}
Grid.swfit
//
// Grid.swift
// Memorize
//
// Created by user on 2021/01/17.
//
import SwiftUI
struct Grid<Item, ItemView>: View where Item: Identifiable, ItemView: View{
private var items: [Item]
private var viewForItem: (Item) -> ItemView
init (_ items: [Item], viewForItem: @escaping (Item)-> ItemView) {
self.items = items
self.viewForItem = viewForItem
}
var body: some View {
GeometryReader { geometry in
body(for: GridLayout(itemCount: items.count, in: geometry.size))
}
}
private func body(for layout: GridLayout) -> some View {
ForEach(items) { item in
body(for: item, in: layout)
}
}
private func body(for item: Item, in layout: GridLayout) -> some View {
let index = items.firstIndex(matching: item)!
// return Group {
// if index != nil {
// viewForItem(item)
// .frame(width: layout.itemSize.width, height: layout.itemSize.height)
// .position(layout.location(ofItemAt: index!))
// }
// }
return viewForItem(item)
.frame(width: layout.itemSize.width, height: layout.itemSize.height)
.position(layout.location(ofItemAt: index))
}
}
MemoryGame.swfit
//
// MemoryGame.swift
// Memorize
//
// Created by user on 2021/01/16.
//
import Foundation
struct MemoryGame<CardContent> where CardContent: Equatable {
private(set) var cards: Array<Card>
private var indexOfTheOneAndOnlyFaceUpCard: Int? {
get {cards.indices.filter { cards[$0].isFaceUp }.only}
set {
for index in cards.indices {
cards[index].isFaceUp = index == newValue
}
}
}
mutating func choose (card: Card){
if let choseIndex = cards.firstIndex(matching: card), !cards[choseIndex].isFaceUp, !cards[choseIndex].isMatched {
if let potentialMatchIndex = indexOfTheOneAndOnlyFaceUpCard {
if cards[choseIndex].content == cards[potentialMatchIndex].content {
cards[choseIndex].isMatched = true
cards[potentialMatchIndex].isMatched = true
}
cards[choseIndex].isFaceUp = true
}else {
indexOfTheOneAndOnlyFaceUpCard = choseIndex
}
}
}
init (numberOfPairsOfCards: Int, cardContentFactory: (Int)-> CardContent) {
cards = Array<Card>()
for pairIndex in 0 ..< numberOfPairsOfCards {
let content = cardContentFactory(pairIndex)
cards.append(Card(content: content, id: pairIndex * 2))
cards.append(Card(content: content, id: pairIndex * 2 + 1 ))
}
cards.shuffle()
}
struct Card: Identifiable {
var isFaceUp: Bool = false {
didSet {
if isFaceUp {
startUsingBonusTime()
}else {
stopUsingBonusTime()
}
}
}
var isMatched: Bool = false {
didSet {
stopUsingBonusTime()
}
}
var content: CardContent
var id: Int
// MARK: Bonus Time
var bonusTimeLimit: TimeInterval = 6
var lastFaceUpDate: Date?
var pastFaceUpTime: TimeInterval = 0
private var faceUpTime: TimeInterval {
if let lastFaceUpDate = self.lastFaceUpDate {
return pastFaceUpTime + Date().timeIntervalSince(lastFaceUpDate)
}else {
return pastFaceUpTime
}
}
var bonusTimeRemaining: TimeInterval {
max(0, bonusTimeLimit - faceUpTime)
}
var bonusRemaining: Double {
(bonusTimeLimit > 0 && bonusTimeRemaining > 0) ? bonusTimeRemaining / bonusTimeLimit : 0
}
var hasEarnedBonus: Bool {
isMatched && bonusTimeRemaining > 0
}
var isConsumingBonusTime: Bool {
isFaceUp && !isMatched && bonusTimeRemaining > 0
}
private mutating func startUsingBonusTime() {
if isConsumingBonusTime, lastFaceUpDate == nil {
lastFaceUpDate = Date()
}
}
private mutating func stopUsingBonusTime() {
pastFaceUpTime = faceUpTime
lastFaceUpDate = nil
}
}
}
EmojiMemoryGame.swfit
//
// EmojiMemoryGame.swift
// Memorize
//
// Created by user on 2021/01/16.
//
import SwiftUI
class EmojiMemoryGame: ObservableObject {
@Published private var model: MemoryGame<String> = EmojiMemoryGame.createMemoryGame()
static func createMemoryGame() -> MemoryGame<String>{
let emojis: Array<String> = ["😊", "😱", "😎"]
return MemoryGame<String>(numberOfPairsOfCards: emojis.count) { parIndex in
return emojis[parIndex]
}
}
// MARK: - Access to the Model
var cards: Array<MemoryGame<String>.Card> {
model.cards
}
// MARK: - Intent(s)
func choose(card: MemoryGame<String>.Card){
model.choose(card: card)
}
func resetGame() {
model = EmojiMemoryGame.createMemoryGame()
}
}
EmojiMemoryGame.swfit
//
// ContentView.swift
// Memorize
//
// Created by user on 2021/01/15.
//
import SwiftUI
struct EmojiMemoryGameView: View {
@ObservedObject var viewModel: EmojiMemoryGame
var body: some View {
VStack{
Grid(viewModel.cards) { card in
CardView(card: card).onTapGesture {
withAnimation(.linear(duration:0.75)){
viewModel.choose(card: card)
}
}
.padding(5)
}
.padding()
.foregroundColor(Color.orange)
Button(action: {
withAnimation(.easeInOut) {
viewModel.resetGame()
}
}, label: {
Text("New Game") // localizedString Key
})
}
}
}
struct CardView: View {
var card: MemoryGame<String>.Card
var body: some View {
GeometryReader(content: { geometry in
body(for: geometry.size)
})
}
@State private var animatedBonusRemaining: Double = 0
private func startBonusTimeAnimation () {
animatedBonusRemaining = card.bonusRemaining
withAnimation(.linear(duration: card.bonusTimeRemaining)){
animatedBonusRemaining = 0
}
}
@ViewBuilder
private func body(for size: CGSize) -> some View {
if card.isFaceUp || !card.isMatched {
ZStack {
Group {
if card.isConsumingBonusTime {
Pie(startAngle: Angle.degrees(0-90), endAngle: Angle.degrees(-animatedBonusRemaining * 360-90), clockwise: true)
.onAppear {
startBonusTimeAnimation()
}
} else {
Pie(startAngle: Angle.degrees(0-90), endAngle: Angle.degrees(-card.bonusRemaining * 360-90), clockwise: true)
}
}
.padding(5)
.opacity(0.4)
.transition(.scale)
Text(card.content)
.font(Font.system(size: fontSize(for: size)))
.rotationEffect(Angle.degrees(card.isMatched ? 360 :0))
.animation(card.isMatched ?Animation.linear(duration: 1).repeatForever(autoreverses: false)
: .default)
}
.cardify(isFaceUp: card.isFaceUp)
.transition(AnyTransition.scale)
}
}
// MARK: - Drawing Constants
private func fontSize(for size: CGSize) -> CGFloat {
min(size.width, size.height) * 0.7
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let game = EmojiMemoryGame()
game.choose(card: game.cards[0])
return EmojiMemoryGameView(viewModel: game)
}
}
Array+Identifiable.swfit
//
// Array+Identifiable.swift
// Memorize
//
// Created by user on 2021/01/17.
//
import Foundation
extension Array where Element: Identifiable {
func firstIndex(matching: Element) -> Int? {
for index in 0..<self.count {
if self[index].id == matching.id {
return index
}
}
return nil
}
}
Array+Only.swfit
//
// Array_Only.swift
// Memorize
//
// Created by user on 2021/01/18.
//
import Foundation
extension Array {
var only: Element? {
count == 1 ? first: nil
}
}
Pie.swfit
//
// Pie.swift
// Memorize
//
// Created by user on 2021/01/18.
//
import SwiftUI
struct Pie: Shape {
var startAngle: Angle
var endAngle: Angle
var clockwise: Bool = false
var animatableData: AnimatablePair<Double, Double> {
get {
AnimatablePair(startAngle.radians, endAngle.radians)
}
set {
startAngle = Angle.radians(newValue.first)
endAngle = Angle.radians(newValue.second)
}
}
func path (in rect: CGRect) -> Path {
let center = CGPoint(x: rect.maxX / 2, y: rect.maxY / 2)
let radius = min(rect.width, rect.height) / 2
let start = CGPoint (
x: center.x + radius * cos(CGFloat(startAngle.radians)),
y: center.y + radius * sin(CGFloat(startAngle.radians))
)
var p = Path()
p.move(to: center)
p.addLine(to: start)
p.addArc (center: center,
radius: radius,
startAngle: startAngle,
endAngle: endAngle,
clockwise: clockwise)
p.addLine(to: center)
return p
}
}
extension View {
func cardify (isFaceUp: Bool) -> some View {
modifier(Cardify(isFaceUp: isFaceUp))
}
}
Cardify.swfit
//
// Cardify.swift
// Memorize
//
// Created by user on 2021/01/18.
//
import SwiftUI
//ViewModifier, Animatable
struct Cardify: AnimatableModifier {
var rotation: Double
init(isFaceUp: Bool) {
rotation = isFaceUp ? 0.0: 180.0
}
var isFaceUp: Bool {
rotation < 90
}
var animatableData: Double {
get {return rotation}
set {rotation = newValue}
}
func body(content: Content) -> some View {
ZStack {
Group {
RoundedRectangle(cornerRadius: conrnerRadius)
.fill(Color.white)
RoundedRectangle(cornerRadius: conrnerRadius)
.stroke(lineWidth: edgeLineWidth)
content
}.opacity(isFaceUp ? 1: 0)
RoundedRectangle(cornerRadius: conrnerRadius)
.fill().opacity(isFaceUp ? 0: 1)
}
.rotation3DEffect(Angle.degrees(rotation),axis: (0,1,0))
}
// MARK: - Drawing Constants
private let conrnerRadius: CGFloat = 10
private let edgeLineWidth: CGFloat = 3
}
'IOS' 카테고리의 다른 글
3. IOS 강의 Gestures JSON (0) | 2021.01.19 |
---|---|
3. IOS 강의 Multithreading EmojiArt (0) | 2021.01.18 |
3. IOS 강의 ViewBuilder Shape ViewModifier (0) | 2021.01.18 |
3. IOS 강의 Grid enum Optionals (0) | 2021.01.18 |
3. IOS 강의 Reactive UI + Protocols + Layout (0) | 2021.01.17 |