Skip to content

Commit a9e4106

Browse files
seedanteseedante
authored and
seedante
committed
en....Xcode can't check another branch unless you save current branch, so...
1 parent b91d5b1 commit a9e4106

File tree

4 files changed

+100
-38
lines changed

4 files changed

+100
-38
lines changed

CardAnimation.xcodeproj/project.pbxproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
FDD71FC31BBB360A00E076EE /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FDD71FC11BBB360A00E076EE /* LaunchScreen.storyboard */; };
1515
FDD71FCE1BBB360A00E076EE /* CardAnimationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDD71FCD1BBB360A00E076EE /* CardAnimationTests.swift */; };
1616
FDD71FD91BBB360A00E076EE /* CardAnimationUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDD71FD81BBB360A00E076EE /* CardAnimationUITests.swift */; };
17-
FDD720211BBBF36700E076EE /* aqualad_young_justice_logo_by_kalangozilla.jpg in Resources */ = {isa = PBXBuildFile; fileRef = FDD7201A1BBBF36700E076EE /* aqualad_young_justice_logo_by_kalangozilla.jpg */; settings = {ASSET_TAGS = (); }; };
17+
FDD720211BBBF36700E076EE /* aquaman_young_justice_logo_by_kalangozilla.jpg in Resources */ = {isa = PBXBuildFile; fileRef = FDD7201A1BBBF36700E076EE /* aquaman_young_justice_logo_by_kalangozilla.jpg */; settings = {ASSET_TAGS = (); }; };
1818
FDD720221BBBF36700E076EE /* batman_tim_burton_style_logo_by_kalangozilla.jpg in Resources */ = {isa = PBXBuildFile; fileRef = FDD7201B1BBBF36700E076EE /* batman_tim_burton_style_logo_by_kalangozilla.jpg */; settings = {ASSET_TAGS = (); }; };
1919
FDD720231BBBF36700E076EE /* classic_captain_marvel_jr_logo_by_kalangozilla.jpg in Resources */ = {isa = PBXBuildFile; fileRef = FDD7201C1BBBF36700E076EE /* classic_captain_marvel_jr_logo_by_kalangozilla.jpg */; settings = {ASSET_TAGS = (); }; };
2020
FDD720241BBBF36700E076EE /* green_lantern_corps_logo_by_kalangozilla.jpg in Resources */ = {isa = PBXBuildFile; fileRef = FDD7201D1BBBF36700E076EE /* green_lantern_corps_logo_by_kalangozilla.jpg */; settings = {ASSET_TAGS = (); }; };
@@ -56,7 +56,7 @@
5656
FDD71FD41BBB360A00E076EE /* CardAnimationUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CardAnimationUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5757
FDD71FD81BBB360A00E076EE /* CardAnimationUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardAnimationUITests.swift; sourceTree = "<group>"; };
5858
FDD71FDA1BBB360A00E076EE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
59-
FDD7201A1BBBF36700E076EE /* aqualad_young_justice_logo_by_kalangozilla.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = aqualad_young_justice_logo_by_kalangozilla.jpg; sourceTree = "<group>"; };
59+
FDD7201A1BBBF36700E076EE /* aquaman_young_justice_logo_by_kalangozilla.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = aquaman_young_justice_logo_by_kalangozilla.jpg; sourceTree = "<group>"; };
6060
FDD7201B1BBBF36700E076EE /* batman_tim_burton_style_logo_by_kalangozilla.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = batman_tim_burton_style_logo_by_kalangozilla.jpg; sourceTree = "<group>"; };
6161
FDD7201C1BBBF36700E076EE /* classic_captain_marvel_jr_logo_by_kalangozilla.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = classic_captain_marvel_jr_logo_by_kalangozilla.jpg; sourceTree = "<group>"; };
6262
FDD7201D1BBBF36700E076EE /* green_lantern_corps_logo_by_kalangozilla.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = green_lantern_corps_logo_by_kalangozilla.jpg; sourceTree = "<group>"; };
@@ -115,7 +115,7 @@
115115
FDD71FB71BBB360A00E076EE /* CardAnimation */ = {
116116
isa = PBXGroup;
117117
children = (
118-
FDD7201A1BBBF36700E076EE /* aqualad_young_justice_logo_by_kalangozilla.jpg */,
118+
FDD7201A1BBBF36700E076EE /* aquaman_young_justice_logo_by_kalangozilla.jpg */,
119119
FDD7201B1BBBF36700E076EE /* batman_tim_burton_style_logo_by_kalangozilla.jpg */,
120120
FDD7201C1BBBF36700E076EE /* classic_captain_marvel_jr_logo_by_kalangozilla.jpg */,
121121
FDD7201D1BBBF36700E076EE /* green_lantern_corps_logo_by_kalangozilla.jpg */,
@@ -261,7 +261,7 @@
261261
FDD720251BBBF36700E076EE /* superman_kingdom_come_logo_by_kalangozilla.jpg in Resources */,
262262
FDD720271BBBF36700E076EE /* wonder_woman_logo_by_kalangozilla.jpg in Resources */,
263263
FDEDBBFD1BBFE9E1009654BD /* JLAFRICA.jpeg in Resources */,
264-
FDD720211BBBF36700E076EE /* aqualad_young_justice_logo_by_kalangozilla.jpg in Resources */,
264+
FDD720211BBBF36700E076EE /* aquaman_young_justice_logo_by_kalangozilla.jpg in Resources */,
265265
FDD71FC01BBB360A00E076EE /* Assets.xcassets in Resources */,
266266
FDD720241BBBF36700E076EE /* green_lantern_corps_logo_by_kalangozilla.jpg in Resources */,
267267
FDD720221BBBF36700E076EE /* batman_tim_burton_style_logo_by_kalangozilla.jpg in Resources */,

CardAnimation/Base.lproj/Main.storyboard

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
<view clipsSubviews="YES" tag="6" contentMode="scaleAspectFit" translatesAutoresizingMaskIntoConstraints="NO" id="oJ8-qS-ALn" userLabel="View6">
8585
<rect key="frame" x="100" y="150" width="400" height="300"/>
8686
<subviews>
87-
<imageView clipsSubviews="YES" userInteractionEnabled="NO" tag="10" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="aqualad_young_justice_logo_by_kalangozilla.jpg" translatesAutoresizingMaskIntoConstraints="NO" id="0q7-di-Sa3">
87+
<imageView clipsSubviews="YES" userInteractionEnabled="NO" tag="10" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="aquaman_young_justice_logo_by_kalangozilla.jpg" translatesAutoresizingMaskIntoConstraints="NO" id="0q7-di-Sa3">
8888
<rect key="frame" x="0.0" y="0.0" width="400" height="300"/>
8989
<animations/>
9090
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
@@ -183,7 +183,7 @@
183183
</imageView>
184184
</subviews>
185185
<animations/>
186-
<color key="backgroundColor" red="0.1334631741" green="0.1823148429" blue="0.25190103050000001" alpha="1" colorSpace="calibratedRGB"/>
186+
<color key="backgroundColor" red="0.13346317410469055" green="0.18231484293937683" blue="0.25190103054046631" alpha="1" colorSpace="calibratedRGB"/>
187187
<constraints>
188188
<constraint firstAttribute="width" secondItem="7aX-YZ-5Oq" secondAttribute="height" multiplier="4:3" id="49m-ls-CCI"/>
189189
<constraint firstItem="sF4-x4-7Rg" firstAttribute="centerX" secondItem="7aX-YZ-5Oq" secondAttribute="centerX" id="Ecb-4N-mzD"/>
@@ -259,12 +259,12 @@
259259
</viewController>
260260
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
261261
</objects>
262-
<point key="canvasLocation" x="558" y="462"/>
262+
<point key="canvasLocation" x="369" y="269"/>
263263
</scene>
264264
</scenes>
265265
<resources>
266266
<image name="JLAFRICA.jpeg" width="840" height="328"/>
267-
<image name="aqualad_young_justice_logo_by_kalangozilla.jpg" width="900" height="563"/>
267+
<image name="aquaman_young_justice_logo_by_kalangozilla.jpg" width="900" height="563"/>
268268
<image name="batman_begins_poster_style_logo_by_kalangozilla.jpg" width="900" height="563"/>
269269
<image name="classic_captain_marvel_jr_logo_by_kalangozilla.jpg" width="900" height="563"/>
270270
<image name="flash_logo_by_kalangozilla.jpg" width="900" height="563"/>

CardAnimation/ViewController.swift

+92-30
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,44 @@
88

99
import UIKit
1010

11+
protocol SDECardSource{
12+
var cardCount: Int {get set}
13+
func cardImageAtIndex(index:Int) -> UIImage?
14+
}
15+
1116
enum panScrollDirection{
1217
case Up, Down
1318
}
1419

20+
enum JusticeLeagueHeroLogo: String{
21+
case WonderWoman = "wonder_woman_logo_by_kalangozilla.jpg"
22+
case Superman = "superman_kingdom_come_logo_by_kalangozilla.jpg"
23+
case Batman = "batman_begins_poster_style_logo_by_kalangozilla.jpg"
24+
case GreenLantern = "green_lantern_corps_logo_by_kalangozilla.jpg"
25+
case Flash = "flash_logo_by_kalangozilla.jpg"
26+
case Aquaman = "aquaman_young_justice_logo_by_kalangozilla.jpg"
27+
case CaptainMarvel = "classic_captain_marvel_jr_logo_by_kalangozilla.jpg"
28+
//can't find Cybord's Logo.
29+
case AllMembers = "JLAFRICA.jpeg"
30+
}
31+
32+
1533
class ViewController: UIViewController {
1634

1735
var frontCardTag = 1
18-
var cardCount = 8
36+
var cardCount = 0
37+
let maxVisibleCardCount = 8
1938
let gradientBackgroundLayer = CAGradientLayer()
20-
var originFrame = CGRectZero
2139
var gestureDirection:panScrollDirection = .Up
40+
var logoArray: [JusticeLeagueHeroLogo] = [.Superman, .WonderWoman, .Batman, .GreenLantern, .Flash, .Aquaman, .CaptainMarvel, .AllMembers]{
41+
didSet{
42+
cardCount = logoArray.count
43+
}
44+
}
2245

2346
@IBOutlet weak var frontCenterYConstraint: NSLayoutConstraint!
2447

48+
//MARK: View Life Management
2549
override func viewDidLoad() {
2650
super.viewDidLoad()
2751

@@ -32,6 +56,7 @@ class ViewController: UIViewController {
3256
let scrollGesture = UIPanGestureRecognizer(target: self, action: "scrollOnView:")
3357
view.addGestureRecognizer(scrollGesture)
3458

59+
cardCount = logoArray.count
3560
relayoutSubViews()
3661
}
3762

@@ -40,7 +65,56 @@ class ViewController: UIViewController {
4065
// Dispose of any resources that can be recreated.
4166
}
4267

68+
//MARK: Data Source
69+
func cardImageAtIndex(index: Int) -> UIImage?{
70+
return UIImage(named: logoArray[index].rawValue)!
71+
}
72+
4373
//MARK: Action Method
74+
@IBAction func addCard(sender: AnyObject) {
75+
let newCardView = createNewCardViewWith(UIImage(named: JusticeLeagueHeroLogo.Batman.rawValue))
76+
view.addSubview(newCardView)
77+
78+
logoArray.append(.Batman)
79+
let YOffset = 0 - calculusYOffsetForIndex(logoArray.count)
80+
let widthConstraint = calculateWidthScaleForIndex(logoArray.count) * view.bounds.size.width
81+
let borderWidth = widthConstraint/100
82+
newCardView.layer.borderColor = UIColor.whiteColor().CGColor
83+
newCardView.layer.borderWidth = borderWidth
84+
//添加layout constraint 必须在 addSubView() 后执行
85+
NSLayoutConstraint(item: newCardView, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: 0).active = true
86+
NSLayoutConstraint(item: newCardView, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: YOffset).active = true
87+
NSLayoutConstraint(item: newCardView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: widthConstraint).active = true
88+
NSLayoutConstraint(item: newCardView, attribute: .Width, relatedBy: .Equal, toItem: newCardView, attribute: .Height, multiplier: 4.0/3.0, constant: 0).active = true
89+
90+
}
91+
92+
func createNewCardViewWith(image: UIImage?) -> UIView{
93+
let newCardView = UIView()
94+
newCardView.translatesAutoresizingMaskIntoConstraints = false
95+
newCardView.backgroundColor = UIColor.brownColor()
96+
newCardView.tag = logoArray.count + 1
97+
newCardView.clipsToBounds = true
98+
newCardView.alpha = calculateAlphaForIndex(logoArray.count + 1 - frontCardTag)
99+
newCardView.layer.zPosition = CGFloat(1000 - logoArray.count - 1 + frontCardTag)
100+
101+
102+
let subImageView = UIImageView(image: image)
103+
subImageView.translatesAutoresizingMaskIntoConstraints = false
104+
subImageView.contentMode = .ScaleAspectFill
105+
subImageView.clipsToBounds = true
106+
subImageView.tag = 10
107+
108+
newCardView.addSubview(subImageView)
109+
NSLayoutConstraint(item: subImageView, attribute: .CenterX, relatedBy: .Equal, toItem: newCardView, attribute: .CenterX, multiplier: 1, constant: 0).active = true
110+
NSLayoutConstraint(item: subImageView, attribute: .CenterY, relatedBy: .Equal, toItem: newCardView, attribute: .CenterY, multiplier: 1, constant: 0).active = true
111+
NSLayoutConstraint(item: subImageView, attribute: .Width, relatedBy: .Equal, toItem: newCardView, attribute: .Width, multiplier: 1, constant: 0).active = true
112+
NSLayoutConstraint(item: subImageView, attribute: .Height, relatedBy: .Equal, toItem: newCardView, attribute: .Height, multiplier: 1, constant: 0).active = true
113+
114+
return newCardView
115+
}
116+
117+
44118
@IBAction func flipUp(sender: AnyObject) {
45119
if frontCardTag == 1{
46120
return
@@ -246,8 +320,13 @@ class ViewController: UIViewController {
246320
func relayoutSubViewWith(viewTag: Int, relativeIndex:Int, delay: NSTimeInterval, haveBorderWidth: Bool){
247321
let width = view.bounds.size.width
248322
if let subView = view.viewWithTag(viewTag){
249-
let alpha = calculateAlphaForIndex(relativeIndex)
250-
subView.alpha = alpha
323+
324+
if let nestedImageView = subView.viewWithTag(10) as? UIImageView{
325+
nestedImageView.image = cardImageAtIndex(viewTag - 1)
326+
}
327+
328+
subView.layer.zPosition = CGFloat(1000 - relativeIndex)
329+
subView.alpha = calculateAlphaForIndex(relativeIndex)
251330

252331
var borderWidth: CGFloat = 0
253332
let filterSubViewConstraints = subView.constraints.filter({$0.firstAttribute == .Width && $0.secondItem == nil})
@@ -281,17 +360,18 @@ class ViewController: UIViewController {
281360

282361
func adjustUpViewLayout(){
283362
if frontCardTag >= 2{
363+
let endCardTag = cardCount - frontCardTag > maxVisibleCardCount - 1 ? (frontCardTag + maxVisibleCardCount - 1) : cardCount
284364
let feed: UInt32 = 2
285365
let randomRoll = arc4random_uniform(feed)
286366
switch randomRoll{
287367
case 0:
288-
for var viewTag = frontCardTag; viewTag <= cardCount; ++viewTag{
368+
for var viewTag = frontCardTag; viewTag <= endCardTag; ++viewTag{
289369
let delay: NSTimeInterval = Double(viewTag - frontCardTag)*0.1
290370
let relativeIndex = viewTag - frontCardTag + 1
291371
relayoutSubViewWith(viewTag, relativeIndex: relativeIndex, delay: delay, haveBorderWidth: true)
292372
}
293373
case 1:
294-
for var viewTag = cardCount; viewTag >= frontCardTag; --viewTag{
374+
for var viewTag = endCardTag; viewTag >= frontCardTag; --viewTag{
295375
let delay: NSTimeInterval = Double(cardCount - viewTag) * 0.1
296376
let relativeIndex = viewTag - frontCardTag + 1
297377
relayoutSubViewWith(viewTag, relativeIndex: relativeIndex, delay: delay, haveBorderWidth: true)
@@ -306,9 +386,9 @@ class ViewController: UIViewController {
306386

307387
func adjustDownViewLayout(){
308388
frontCardTag += 1
309-
310-
if frontCardTag <= cardCount{
311-
for viewTag in frontCardTag...cardCount{
389+
let endCardTag = cardCount - frontCardTag > maxVisibleCardCount - 1 ? (frontCardTag + maxVisibleCardCount - 1) : cardCount
390+
if frontCardTag <= endCardTag{
391+
for viewTag in frontCardTag...endCardTag{
312392
let delay: NSTimeInterval = 0.1 * Double(viewTag - frontCardTag)
313393
let relativeIndex = viewTag - frontCardTag
314394
relayoutSubViewWith(viewTag, relativeIndex: relativeIndex, delay: delay, haveBorderWidth: true)
@@ -317,8 +397,9 @@ class ViewController: UIViewController {
317397
}
318398

319399
func relayoutSubViews(){
320-
if frontCardTag <= cardCount{
321-
for viewTag in frontCardTag...cardCount{
400+
let endCardTag = cardCount - frontCardTag > maxVisibleCardCount - 1 ? (frontCardTag + maxVisibleCardCount - 1) : cardCount
401+
if frontCardTag <= endCardTag{
402+
for viewTag in frontCardTag...endCardTag{
322403
if let subView = view.viewWithTag(viewTag){
323404
subView.layer.anchorPoint = CGPointMake(0.5, 1)
324405
let relativeIndex = viewTag - frontCardTag
@@ -345,25 +426,6 @@ class ViewController: UIViewController {
345426
}
346427

347428
//MARK: Helper Method
348-
func setAnchorPoint(anchorPoint: CGPoint, forView view: UIView) {
349-
var newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y)
350-
var oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y)
351-
352-
newPoint = CGPointApplyAffineTransform(newPoint, view.transform)
353-
oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform)
354-
355-
var position = view.layer.position
356-
position.x -= oldPoint.x
357-
position.x += newPoint.x
358-
359-
position.y -= oldPoint.y
360-
position.y += newPoint.y
361-
362-
view.layer.position = position
363-
view.layer.anchorPoint = anchorPoint
364-
}
365-
366-
367429
//f(x) = k * x + m
368430
func calculateFactorOfFunction(x1: CGFloat, x2: CGFloat, y1: CGFloat, y2: CGFloat) -> (CGFloat, CGFloat){
369431

0 commit comments

Comments
 (0)