Skip to content

Commit

Permalink
Refactor: Modify ViewController, Add CustomEnum (Resolve #5, #6)
Browse files Browse the repository at this point in the history
- Arrange code order and Delete unused code
- Add annotations
- Declare variables for drawing and set up cardImage frames
  • Loading branch information
jinios committed Apr 17, 2018
1 parent 7b8179a commit 8a30166
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 57 deletions.
4 changes: 4 additions & 0 deletions CardGameApp/CardGameApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
FDD8FA1D207F035500F5FD99 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FDD8FA1C207F035500F5FD99 /* Assets.xcassets */; };
FDD8FA20207F035500F5FD99 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FDD8FA1E207F035500F5FD99 /* LaunchScreen.storyboard */; };
FDD8FA28207F216B00F5FD99 /* CardImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDD8FA27207F216B00F5FD99 /* CardImageView.swift */; };
FDDD33C62085EFCF000E9D0E /* CustomEnum.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDDD33C52085EFCF000E9D0E /* CustomEnum.swift */; };
FDDFB92A2080472500920FB4 /* card_decks in Resources */ = {isa = PBXBuildFile; fileRef = FDDFB9292080472500920FB4 /* card_decks */; };
FDDFB92E2080497400920FB4 /* ResultData.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDDFB92D2080497400920FB4 /* ResultData.swift */; };
FDDFB93A2080498800920FB4 /* CardStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDDFB9332080498700920FB4 /* CardStack.swift */; };
Expand All @@ -32,6 +33,7 @@
FDD8FA1F207F035500F5FD99 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
FDD8FA21207F035500F5FD99 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
FDD8FA27207F216B00F5FD99 /* CardImageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardImageView.swift; sourceTree = "<group>"; };
FDDD33C52085EFCF000E9D0E /* CustomEnum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomEnum.swift; sourceTree = "<group>"; };
FDDFB9292080472500920FB4 /* card_decks */ = {isa = PBXFileReference; lastKnownFileType = folder; path = card_decks; sourceTree = "<group>"; };
FDDFB92D2080497400920FB4 /* ResultData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResultData.swift; sourceTree = "<group>"; };
FDDFB9332080498700920FB4 /* CardStack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardStack.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -80,6 +82,7 @@
FDD8FA1E207F035500F5FD99 /* LaunchScreen.storyboard */,
FDD8FA21207F035500F5FD99 /* Info.plist */,
FDD8FA27207F216B00F5FD99 /* CardImageView.swift */,
FDDD33C52085EFCF000E9D0E /* CustomEnum.swift */,
);
path = CardGameApp;
sourceTree = "<group>";
Expand Down Expand Up @@ -171,6 +174,7 @@
FDABC82F20804F8F00AE6DE5 /* ImageSelector.swift in Sources */,
FDDFB92E2080497400920FB4 /* ResultData.swift in Sources */,
FDD8FA18207F035400F5FD99 /* ViewController.swift in Sources */,
FDDD33C62085EFCF000E9D0E /* CustomEnum.swift in Sources */,
FDDFB93C2080498800920FB4 /* CardDeck.swift in Sources */,
FDD8FA28207F216B00F5FD99 /* CardImageView.swift in Sources */,
FDDFB93D2080498800920FB4 /* Card.swift in Sources */,
Expand Down
45 changes: 45 additions & 0 deletions CardGameApp/CardGameApp/CustomEnum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// CustomEnum.swift
// CardGameApp
//
// Created by YOUTH2 on 2018. 4. 17..
// Copyright © 2018년 JINiOS. All rights reserved.
//

import Foundation
import UIKit

enum PositionX: EnumCollection {
case first
case second
case third
case fourth
case fifth
case sixth
case seventh

var value: CGFloat {
switch self {
case .first: return 6.46875
case .second: return 64.6875
case .third: return 122.90625
case .fourth: return 181.125
case .fifth: return 239.34375
case .sixth: return 297.5625
case .seventh: return 355.78125
}
}

}

enum PositionY {
case upper
case bottom

var value: CGFloat {
switch self {
case .upper: return 20
case .bottom: return 100
}
}
}
123 changes: 66 additions & 57 deletions CardGameApp/CardGameApp/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import UIKit
class ViewController: UIViewController {

var cardDeck = CardDeck()
var cardDeckView: CardImageView!

let widthDivider: CGFloat = 8
let cardHeightRatio: CGFloat = 1.27
let foundationPositionY: CGFloat = 20
let cardPositionY: CGFloat = 100
let numberOfDeck = 7
let numberOfFoundation = 4
var cardDeckView = CardImageView()
let foundationPositionY: CGFloat = PositionY.upper.value
let cardPositionY: CGFloat = PositionY.bottom.value

var cardWidth: CGFloat {
return self.view.frame.width / widthDivider
Expand All @@ -29,48 +29,50 @@ class ViewController: UIViewController {
}

var space: CGFloat {
return cardWidth / 8
return cardWidth / widthDivider
}

var spaceY: CGFloat = 15.0

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "bg_pattern")!)
self.view.isUserInteractionEnabled = true
self.initialView()
}

private func initialView() {
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "bg_pattern")!)
self.drawStacks()
self.defaultDeck()
self.drawFoundations()
self.drawDeck()
self.setGestureToCardDeckView()
self.setGestureToCardDeck()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: InitialView Related

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

// 카드뒷면출력
private func drawCards() {
cardDeck.shuffle()
let cards = cardDeck.makeCards(numberOfDeck)
for i in 0..<numberOfDeck {
let cardX = (CGFloat(i+1)*space) + (CGFloat(i) * cardWidth)
let cardImage = CardImageView()
cardImage.getImage(of: cards[i])
cardImage.frame = CGRect(origin: CGPoint(x: cardX, y: cardPositionY), size: self.cardSize)
self.view.addSubview(cardImage)
private func defaultDeck() {
cardDeckView = CardImageView(frame: CGRect(origin: CGPoint(x: PositionX.seventh.value,
y: PositionY.upper.value),
size: self.cardSize))
if cardDeck.count() > 0 {
cardDeckView.getBackSideImage()
self.view.addSubview(cardDeckView)
} else {
cardDeckView.getRefreshImage()
self.view.addSubview(cardDeckView)
}
}


private func drawFoundations() {
for i in 0..<numberOfFoundation {
let cardX = (CGFloat(i+1)*space) + (CGFloat(i) * cardWidth)
let foundation = UIView(frame: CGRect(origin: CGPoint(x: cardX, y: foundationPositionY), size: self.cardSize))
let foundation = UIView(frame: CGRect(origin: CGPoint(x: cardX,
y: foundationPositionY),
size: self.cardSize))
foundation.clipsToBounds = true
foundation.layer.cornerRadius = 5.0
foundation.layer.borderColor = UIColor.white.cgColor
Expand All @@ -79,19 +81,6 @@ class ViewController: UIViewController {
}
}

private func setGestureToCardDeckView() {
cardDeckView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(deckTapped(sender:)))
self.cardDeckView.addGestureRecognizer(tap)
}

@objc func deckTapped(sender : UITapGestureRecognizer) {
print(self.cardDeck.description)
if sender.state == .ended {
self.drawPickedCard()
}
}

private func makeStacks() -> [CardStack] {
var stacks = [CardStack]()
for i in 1...7 {
Expand All @@ -105,38 +94,56 @@ class ViewController: UIViewController {
}

private func drawStacks() {
let locationX: [CGFloat] = [6.46875, 64.6875, 122.90625, 181.125, 239.34375, 297.5625, 355.78125]
let stacks = self.makeStacks()

for i in 0..<7 {
for j in 0..<stacks[i].cards.count {
let locationY = cardPositionY + (15 * CGFloat(j))
let cardInStack = CardImageView(frame: CGRect(origin: CGPoint(x: locationX[i], y: locationY), size: self.cardSize))
let locationY = cardPositionY + (spaceY * CGFloat(j))
let cardInStack = CardImageView(frame: CGRect(origin: CGPoint(x: PositionX.allValues[i].value,
y: locationY),
size: self.cardSize))
cardInStack.getImage(of: stacks[i].cards[j])
self.view.addSubview(cardInStack)
}
}
}

// MARK: Tap Gesture Related

private func setGestureToCardDeck() {
let tap = UITapGestureRecognizer(target: self, action: #selector(deckTapped(sender:)))
self.cardDeckView.addGestureRecognizer(tap)
}

@objc func deckTapped(sender : UITapGestureRecognizer) {
print(self.cardDeck.description)
if sender.state == .ended {
self.drawPickedCard()
}
}

// 카드앞면출력
private func drawPickedCard() {
let upperRightCornerX = 297.5625
let upperRightCornerY = 20.0
let frontCardView = CardImageView(frame: CGRect(origin: CGPoint(x: upperRightCornerX, y: upperRightCornerY), size: self.cardSize))
let picked = cardDeck.removeLast()
picked.turnOver()
frontCardView.getImage(of: picked)
self.view.addSubview(frontCardView)
if cardDeck.count() > 0 {
self.pickCardFromDeck()
} else {
cardDeckView.getRefreshImage()
}
}

private func drawDeck() {
let upperRightCornerX = 355.78125
let upperRightCornerY = 20.0
cardDeckView = CardImageView(frame: CGRect(origin: CGPoint(x: upperRightCornerX, y: upperRightCornerY), size: self.cardSize))
cardDeckView.getBackSideImage()
self.view.addSubview(cardDeckView)
private func pickCardFromDeck() {
let upperRightCornerX = PositionX.sixth.value
let upperRightCornerY = PositionY.upper.value
let pickedCardView = CardImageView(frame: CGRect(origin: CGPoint(x: upperRightCornerX,
y: upperRightCornerY),
size: self.cardSize))
let pickedCard = cardDeck.removeOne()
pickedCard.turnOver()
pickedCardView.getImage(of: pickedCard)
self.view.addSubview(pickedCardView)
}

// MARK: Shake motion Related

override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
if motion == .motionShake {
view.subviews.forEach() { $0.removeFromSuperview() }
Expand All @@ -145,5 +152,7 @@ class ViewController: UIViewController {
}
}


}


0 comments on commit 8a30166

Please sign in to comment.