Skip to content

Commit

Permalink
Implemented card rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
fakerabbit committed Mar 7, 2017
1 parent d853d3e commit d794ec9
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 6 deletions.
12 changes: 12 additions & 0 deletions Memoria.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
AC094F6F1E6F1172006D87F9 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AC094F6E1E6F1172006D87F9 /* QuartzCore.framework */; };
AC2566401E69E5910056895D /* CircleTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC25663F1E69E5910056895D /* CircleTextView.swift */; };
AC2566431E6A066C0056895D /* CreateCardVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC2566421E6A066C0056895D /* CreateCardVC.swift */; };
AC2566451E6A06760056895D /* CreateCardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC2566441E6A06760056895D /* CreateCardView.swift */; };
Expand Down Expand Up @@ -48,6 +49,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
AC094F6E1E6F1172006D87F9 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
AC25663F1E69E5910056895D /* CircleTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CircleTextView.swift; sourceTree = "<group>"; };
AC2566421E6A066C0056895D /* CreateCardVC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CreateCardVC.swift; sourceTree = "<group>"; };
AC2566441E6A06760056895D /* CreateCardView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CreateCardView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -95,12 +97,21 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
AC094F6F1E6F1172006D87F9 /* QuartzCore.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
AC094F6D1E6F1172006D87F9 /* Frameworks */ = {
isa = PBXGroup;
children = (
AC094F6E1E6F1172006D87F9 /* QuartzCore.framework */,
);
name = Frameworks;
sourceTree = "<group>";
};
AC2566411E6A064F0056895D /* Create Card */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -155,6 +166,7 @@
children = (
AC9CBB2F1E64E2F700C634F3 /* Memoria */,
AC9CBB2E1E64E2F700C634F3 /* Products */,
AC094F6D1E6F1172006D87F9 /* Frameworks */,
);
sourceTree = "<group>";
};
Expand Down
4 changes: 2 additions & 2 deletions Memoria/TestButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TestButton: UIButton {
self.backgroundColor = Utils.blueShadowColor()
self.layer.cornerRadius = 18.0
self.layer.borderWidth = 1
self.layer.borderColor = Utils.aquaColor().cgColor
self.layer.borderColor = Utils.cardAlternateColor().cgColor
self.clipsToBounds = true
self.setTitleColor(Utils.textColor(), for: .normal)
}
Expand All @@ -40,7 +40,7 @@ class TestButton: UIButton {
didSet {
switch isHighlighted {
case true:
self.backgroundColor = Utils.aquaColor()
self.backgroundColor = Utils.cardAlternateColor()
case false:
self.backgroundColor = Utils.blueShadowColor()
}
Expand Down
9 changes: 8 additions & 1 deletion Memoria/TestCard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TestCard: UIView {
self.backgroundColor = UIColor.white
self.layer.shadowOpacity = 1.0
self.layer.cornerRadius = 24.0
self.layer.borderColor = Utils.aquaColor().cgColor
self.layer.borderColor = Utils.cardAlternateColor().cgColor
self.layer.borderWidth = 1
self.clipsToBounds = true

Expand All @@ -56,4 +56,11 @@ class TestCard: UIView {
let textS: CGSize = textView.sizeThatFits(CGSize(width: w, height: h))
self.textView.frame = CGRect(x: w/2 - textS.width/2, y: h/2 - textS.height/2, width: textS.width, height: textS.height)
}

// MARK:- Animations

func flipToView(frontCard: TestCard, backCard: TestCard, callback: ((Bool) -> Void)?) {

UIView.transition(from: frontCard, to: backCard, duration: 1.0, options: .transitionFlipFromRight, completion: callback)
}
}
3 changes: 2 additions & 1 deletion Memoria/TestVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class TestVC: UIViewController {
var testCards:[Card?] = self.cards.shuffled()
let card: Card? = testCards.popLast()!
self.testView.title = "Question"
self.testView.card.text = card?.question
self.testView.frontCard.text = card?.question
self.testView.backCard.text = card?.answer

}
}
66 changes: 64 additions & 2 deletions Memoria/TestView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation
import UIKit
import QuartzCore

class TestView: UIView {

Expand All @@ -25,6 +26,34 @@ class TestView: UIView {
lazy var showAnswerBtn: TestButton! = {
let b = TestButton(frame: CGRect.zero)
b.title = "Show Answer"
b.addTarget(self, action: #selector(onShowAnswer(_:)), for: .touchUpInside)
return b
}()

lazy var easyBtn: TestButton! = {
let b = TestButton(frame: CGRect.zero)
b.title = "Easy"
b.isHidden = true
b.isUserInteractionEnabled = false
//b.addTarget(self, action: #selector(onShowAnswer(_:)), for: .touchUpInside)
return b
}()

lazy var goodBtn: TestButton! = {
let b = TestButton(frame: CGRect.zero)
b.title = "Good"
b.isHidden = true
b.isUserInteractionEnabled = false
//b.addTarget(self, action: #selector(onShowAnswer(_:)), for: .touchUpInside)
return b
}()

lazy var hardBtn: TestButton! = {
let b = TestButton(frame: CGRect.zero)
b.title = "Hard"
b.isHidden = true
b.isUserInteractionEnabled = false
// b.addTarget(self, action: #selector(onShowAnswer(_:)), for: .touchUpInside)
return b
}()

Expand All @@ -44,7 +73,17 @@ class TestView: UIView {
return b
}()

lazy var card: TestCard! = {
lazy var frontCard: TestCard! = {
let c = TestCard(frame: CGRect.zero)
return c
}()

lazy var backCard: TestCard! = {
let c = TestCard(frame: CGRect.zero)
return c
}()

private lazy var card: TestCard! = {
let c = TestCard(frame: CGRect.zero)
return c
}()
Expand All @@ -70,6 +109,10 @@ class TestView: UIView {
self.addSubview(titleLbl)
self.addSubview(closeBtn)
self.addSubview(showAnswerBtn)
self.addSubview(easyBtn)
self.addSubview(goodBtn)
self.addSubview(hardBtn)
card.addSubview(frontCard)
}

required init?(coder aDecoder: NSCoder) {
Expand All @@ -84,12 +127,18 @@ class TestView: UIView {
let pad: CGFloat = 30.0
let cardH: CGFloat = h/2
let showS: CGFloat = 140
let smallBtnS: CGFloat = showS/2
let btnH: CGFloat = 50
gradientLayer.frame = self.bounds
card.frame = CGRect(x: pad, y: h/2 - cardH/2, width: w - pad * 2, height: cardH)
frontCard.frame = card.bounds
backCard.frame = card.bounds
closeBtn.frame = CGRect(x: w - (pad + btnS), y: card.frame.minY - btnS, width: btnS, height: btnS)
titleLbl.frame = CGRect(x: pad, y: closeBtn.frame.midY - titleLbl.frame.size.height/2, width: closeBtn.frame.minX - pad, height: titleLbl.frame.size.height)
showAnswerBtn.frame = CGRect(x: w/2 - showS/2, y: card.frame.maxY + 10, width: showS, height: btnH)
goodBtn.frame = CGRect(x: w/2 - smallBtnS/2, y: showAnswerBtn.frame.minY, width: smallBtnS, height: btnH)
easyBtn.frame = CGRect(x: w * 0.25 - smallBtnS/2, y: goodBtn.frame.minY, width: smallBtnS, height: btnH)
hardBtn.frame = CGRect(x: goodBtn.frame.minX + (goodBtn.frame.minX - easyBtn.frame.minX), y: goodBtn.frame.minY, width: smallBtnS, height: btnH)
}

// MARK:- Private
Expand All @@ -99,12 +148,25 @@ class TestView: UIView {
}

func onShowAnswer(_ sender : UIButton) {

self.card.flipToView(frontCard: frontCard, backCard: backCard) { [weak self] finished in

self?.title = "Answer"
self?.showAnswerBtn.isHidden = true
self?.showAnswerBtn.isUserInteractionEnabled = false
self?.easyBtn.isHidden = false
self?.easyBtn.isUserInteractionEnabled = true
self?.goodBtn.isHidden = false
self?.goodBtn.isUserInteractionEnabled = true
self?.hardBtn.isHidden = false
self?.hardBtn.isUserInteractionEnabled = true
}
}

// MARK:- Animations

func randomColors() -> [CGColor] {
let colors = [Utils.aquaColor().cgColor, Utils.blueShadowColor().cgColor]
let colors = [Utils.cardColor().cgColor, Utils.cardAlternateColor().cgColor, Utils.backgroundColor().cgColor]
return colors.shuffled()
}

Expand Down

0 comments on commit d794ec9

Please sign in to comment.