-
Notifications
You must be signed in to change notification settings - Fork 0
/
NGAccordionView.swift
136 lines (98 loc) · 5.28 KB
/
NGAccordionView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
// NGAccordionView.swift
//
// Created by Nick Gorman on 2014-06-09.
// Copyright (c) 2014 Nick Gorman. All rights reserved.
//
import Foundation
import UIKit
class NGAccordionView: UIScrollView {
var items:NSMutableArray = [];
var clipHeight:NSNumber = 0;
var currentlySelectedItem:Int = -1;
override init(frame: CGRect) {
super.init(frame: frame);
self.clipsToBounds = true;
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addObjectToAccordion(clip: NGAccordionBarView) {
clipHeight = clip.frame.size.height;
var itemPosition:NSNumber = items.count as Int * clipHeight.integerValue;
var itemWidth:NSNumber = clip.frame.size.width;
var itemHeight:NSNumber = clip.frame.size.height;
clip.frame = CGRect(x: 0, y: itemPosition.integerValue, width: itemWidth.integerValue, height: itemHeight.integerValue);
self.addSubview(clip);
items.addObject(clip);
clip.btn.addTarget(self, action: Selector("buttonCallback:"), forControlEvents: UIControlEvents.TouchUpInside);
clip.btn.tag = items.count-1;
self.contentSize = CGSize(width: itemWidth.integerValue, height: itemPosition.integerValue);
}
func buttonCallback(obj: UIButton) {
self.animateOpen(obj.tag);
}
func animateOpen(num: Int) {
var bar:NGAccordionBarView = items.objectAtIndex(num) as NGAccordionBarView;
var heightOfOpenBox:Int = bar.fullHeight;
var totalHeightForContainer:Int = 0;
var yPosForItem:Int = 0;
var heightOfSelectedBox:Int = 0;
for ( var i = 0; i < items.count; i++ ){
if ( i != num ){
var midBar:NGAccordionBarView = items.objectAtIndex(i) as NGAccordionBarView;
midBar.spinArrow(0);
var targetHeight:Int = midBar.minHeight;
var newYPosition:Int = (targetHeight * i );
if ( i > num ){
newYPosition = newYPosition + heightOfOpenBox - targetHeight;
}
UIView.beginAnimations(nil, context: nil );
UIView.setAnimationDuration(0.3);
UIView.setAnimationBeginsFromCurrentState(true);
UIView.setAnimationCurve(UIViewAnimationCurve.EaseInOut);
midBar.frame = CGRect(x: midBar.frame.origin.x, y: CGFloat(newYPosition), width: midBar.frame.size.width, height: CGFloat(targetHeight));
UIView.commitAnimations();
totalHeightForContainer = totalHeightForContainer + targetHeight;
} else {
var targetHeight:Int = bar.fullHeight;
var newYPosition:Int = (bar.minHeight * num);
yPosForItem = newYPosition;
heightOfSelectedBox = targetHeight;
bar.spinArrow(180);
UIView.beginAnimations(nil, context: nil );
UIView.setAnimationDuration(0.3);
UIView.setAnimationBeginsFromCurrentState(true);
UIView.setAnimationCurve(UIViewAnimationCurve.EaseInOut);
// close the view
if ( currentlySelectedItem == num ){
bar.spinArrow(0);
currentlySelectedItem = -1;
bar.frame = CGRect(x: bar.frame.origin.x, y: CGFloat(newYPosition), width: bar.frame.size.width, height: CGFloat(bar.minHeight));
heightOfOpenBox = bar.minHeight;
totalHeightForContainer = totalHeightForContainer + bar.minHeight;
heightOfSelectedBox = bar.minHeight;
// open the proper view
} else {
currentlySelectedItem = num
bar.frame = CGRect(x: bar.frame.origin.x, y: CGFloat(newYPosition), width: bar.frame.size.width, height: CGFloat(targetHeight));
totalHeightForContainer = totalHeightForContainer + targetHeight;
heightOfSelectedBox = bar.minHeight;
}
UIView.commitAnimations();
}
}
var newYPos:Int = bar.minHeight * num;
UIView.beginAnimations(nil, context: nil );
UIView.setAnimationDuration(0.3);
UIView.setAnimationBeginsFromCurrentState(true);
UIView.setAnimationCurve(UIViewAnimationCurve.EaseInOut);
self.contentSize = CGSize(width: self.frame.size.width, height: CGFloat(totalHeightForContainer));
UIView.commitAnimations();
var timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: Selector("setRect:"), userInfo: newYPos, repeats: false);
}
func setRect(timer: NSTimer) {
var vw:UIView = self.superview!;
self.scrollRectToVisible(CGRect(x: 0, y: CGFloat(timer.userInfo as NSNumber), width: self.frame.size.width, height: vw.frame.size.height), animated: true);
}
}