Skip to content
Newer
Older
100644 193 lines (140 sloc) 4.74 KB
f43ac45 @soffes Started on collection view
soffes authored
1 //
2 // SSCollectionViewItem.m
3 // SSToolkit
4 //
5 // Created by Sam Soffes on 6/11/10.
e8c3ce3 @soffes Updated comments and renamed SSDrawingMacros to SSDrawingUtilities
soffes authored
6 // Copyright 2010-2011 Sam Soffes. All rights reserved.
f43ac45 @soffes Started on collection view
soffes authored
7 //
8
9 #import "SSCollectionViewItem.h"
137e756 @soffes Started on selection
soffes authored
10 #import "SSCollectionViewItemInternal.h"
a12ab59 @soffes Initial reuse strategy
soffes authored
11 #import "SSCollectionView.h"
12 #import "SSCollectionViewInternal.h"
9947d6f @soffes Added CGSizeAspectScaleToSize
soffes authored
13 #import "SSLabel.h"
e8c3ce3 @soffes Updated comments and renamed SSDrawingMacros to SSDrawingUtilities
soffes authored
14 #import "SSDrawingUtilities.h"
f43ac45 @soffes Started on collection view
soffes authored
15
454e654 @soffes Require Apple LLVM compiler 3.0
soffes authored
16 @implementation SSCollectionViewItem {
17 SSCollectionViewItemStyle _style;
18 }
19
f43ac45 @soffes Started on collection view
soffes authored
20
bf2d0ec @soffes Started overhauling documentation
soffes authored
21 #pragma mark - Accessors
62be25e @soffes Updated coding style and started on extremitiesStyle
soffes authored
22
cf7dc52 @soffes Added SSCollectionViewItemStyleImage to SSCollectionViewItem and swit…
soffes authored
23 @synthesize imageView = _imageView;
f43ac45 @soffes Started on collection view
soffes authored
24 @synthesize textLabel = _textLabel;
25 @synthesize detailTextLabel = _detailTextLabel;
26 @synthesize backgroundView = _backgroundView;
27 @synthesize selectedBackgroundView = _selectedBackgroundView;
28 @synthesize reuseIdentifier = _reuseIdentifier;
29 @synthesize selected = _selected;
30 @synthesize highlighted = _highlighted;
7a67133 @soffes Added selection
soffes authored
31 @synthesize indexPath = _indexPath;
137e756 @soffes Started on selection
soffes authored
32 @synthesize collectionView = _collectionView;
f43ac45 @soffes Started on collection view
soffes authored
33
19937bf @soffes Implement `backgroundView` and `selectedBackgroundView` in SSCollecti…
soffes authored
34 - (void)setBackgroundView:(UIView *)backgroundView {
35 _backgroundView = backgroundView;
3cb6fbe @soffes Fixed SSCollectionViewItem backgroundView bug
soffes authored
36 _backgroundView.hidden = _selected && _selectedBackgroundView;
19937bf @soffes Implement `backgroundView` and `selectedBackgroundView` in SSCollecti…
soffes authored
37
38 [self insertSubview:backgroundView atIndex:0];
600465f @soffes Improve layout
soffes authored
39 [self setNeedsLayout];
19937bf @soffes Implement `backgroundView` and `selectedBackgroundView` in SSCollecti…
soffes authored
40 }
41
42
43 - (void)setSelectedBackgroundView:(UIView *)selectedBackgroundView {
c893cb4 @soffes ARC
soffes authored
44 _selectedBackgroundView = selectedBackgroundView;
19937bf @soffes Implement `backgroundView` and `selectedBackgroundView` in SSCollecti…
soffes authored
45 _selectedBackgroundView.hidden = !_selected;
46
47 if (_backgroundView) {
48 [self insertSubview:_selectedBackgroundView aboveSubview:_backgroundView];
49 } else {
50 [self insertSubview:_selectedBackgroundView atIndex:0];
51 }
600465f @soffes Improve layout
soffes authored
52
53 [self setNeedsLayout];
19937bf @soffes Implement `backgroundView` and `selectedBackgroundView` in SSCollecti…
soffes authored
54 }
55
56
bf2d0ec @soffes Started overhauling documentation
soffes authored
57 #pragma mark - NSObject
f43ac45 @soffes Started on collection view
soffes authored
58
59 - (void)dealloc {
7a67133 @soffes Added selection
soffes authored
60 self.collectionView = nil;
f43ac45 @soffes Started on collection view
soffes authored
61 }
62
63
bf2d0ec @soffes Started overhauling documentation
soffes authored
64 #pragma mark - UIResponder
137e756 @soffes Started on selection
soffes authored
65
66 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
67 [self setHighlighted:YES animated:NO];
68 }
69
70
71 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
72 [self setHighlighted:NO animated:NO];
73 }
74
75
76 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
77 [self setHighlighted:YES animated:NO];
78
600465f @soffes Improve layout
soffes authored
79 if (CGRectContainsPoint(self.bounds, [[touches anyObject] locationInView:self])) {
7fb1b57 @jsy call deselectItemAtIndexPath in touchesEnded to toggle selection state
jsy authored
80 if (self.isSelected) {
81 [self.collectionView deselectItemAtIndexPath:self.indexPath animated:YES];
82 } else {
83 [self.collectionView selectItemAtIndexPath:self.indexPath animated:YES scrollPosition:SSCollectionViewScrollPositionNone];
84 }
137e756 @soffes Started on selection
soffes authored
85 }
86 }
87
88
bf2d0ec @soffes Started overhauling documentation
soffes authored
89 #pragma mark - UIView
f43ac45 @soffes Started on collection view
soffes authored
90
91 - (id)initWithFrame:(CGRect)frame {
92 return [self initWithStyle:SSCollectionViewItemStyleDefault reuseIdentifier:nil];
93 }
94
95
cf7dc52 @soffes Added SSCollectionViewItemStyleImage to SSCollectionViewItem and swit…
soffes authored
96 - (void)layoutSubviews {
600465f @soffes Improve layout
soffes authored
97 _backgroundView.frame = self.bounds;
98 _selectedBackgroundView.frame = self.bounds;
99
cf7dc52 @soffes Added SSCollectionViewItemStyleImage to SSCollectionViewItem and swit…
soffes authored
100 if (_style == SSCollectionViewItemStyleImage) {
600465f @soffes Improve layout
soffes authored
101 _imageView.frame = self.bounds;
a541bc7 @soffes Updated SSCollectionViewItem default sizing
soffes authored
102 } else if (_style == SSCollectionViewItemStyleDefault) {
103 _textLabel.frame = self.bounds;
cf7dc52 @soffes Added SSCollectionViewItemStyleImage to SSCollectionViewItem and swit…
soffes authored
104 }
105 }
106
107
bf2d0ec @soffes Started overhauling documentation
soffes authored
108 #pragma mark - SSCollectionViewItem
f43ac45 @soffes Started on collection view
soffes authored
109
110 - (id)initWithStyle:(SSCollectionViewItemStyle)style reuseIdentifier:(NSString *)aReuseIdentifier {
111 if ((self = [super initWithFrame:CGRectZero])) {
112 _style = style;
113 _reuseIdentifier = [aReuseIdentifier copy];
4795518 @soffes Started on SSCollectionViewItem
soffes authored
114
cf7dc52 @soffes Added SSCollectionViewItemStyleImage to SSCollectionViewItem and swit…
soffes authored
115 if (_style != SSCollectionViewItemStyleBlank) {
a541bc7 @soffes Updated SSCollectionViewItem default sizing
soffes authored
116 if (_style == SSCollectionViewItemStyleSubtitle) {
117 _detailTextLabel = [[SSLabel alloc] initWithFrame:CGRectZero];
118 _detailTextLabel.textAlignment = UITextAlignmentCenter;
119 [self addSubview:_detailTextLabel];
120 }
cf7dc52 @soffes Added SSCollectionViewItemStyleImage to SSCollectionViewItem and swit…
soffes authored
121
9947d6f @soffes Added CGSizeAspectScaleToSize
soffes authored
122 _textLabel = [[SSLabel alloc] initWithFrame:CGRectZero];
cf7dc52 @soffes Added SSCollectionViewItemStyleImage to SSCollectionViewItem and swit…
soffes authored
123 _textLabel.textAlignment = UITextAlignmentCenter;
124 [self addSubview:_textLabel];
26da13d @soffes Added image demo to collection view and vendored EGOImageLoading (sin…
soffes authored
125
126 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
127 [self addSubview:_imageView];
cf7dc52 @soffes Added SSCollectionViewItemStyleImage to SSCollectionViewItem and swit…
soffes authored
128 }
129
f43ac45 @soffes Started on collection view
soffes authored
130 }
131 return self;
132 }
133
134
135 - (void)prepareForReuse {
a12ab59 @soffes Initial reuse strategy
soffes authored
136 // Do nothing. Subclasses can override this
f43ac45 @soffes Started on collection view
soffes authored
137 }
138
139
140 - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
1c4b9e2 @soffes Basic collection view working
soffes authored
141 _selected = selected;
c267557 @soffes Add support for highlighting and selecting to SSCollectionViewItem
soffes authored
142
19937bf @soffes Implement `backgroundView` and `selectedBackgroundView` in SSCollecti…
soffes authored
143 void (^changes)(void) = ^{
144 for (UIView *view in [self subviews]) {
145 if ([view respondsToSelector:@selector(setSelected:)]) {
146 [(UIControl *)view setSelected:_selected];
147 }
c267557 @soffes Add support for highlighting and selecting to SSCollectionViewItem
soffes authored
148 }
19937bf @soffes Implement `backgroundView` and `selectedBackgroundView` in SSCollecti…
soffes authored
149
3cb6fbe @soffes Fixed SSCollectionViewItem backgroundView bug
soffes authored
150 _backgroundView.hidden = _selected && _selectedBackgroundView;
19937bf @soffes Implement `backgroundView` and `selectedBackgroundView` in SSCollecti…
soffes authored
151 _selectedBackgroundView.hidden = !_selected;
152 };
153
154 if (animated) {
155 [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:changes completion:nil];
156 } else {
157 changes();
158 }
f43ac45 @soffes Started on collection view
soffes authored
159 }
160
161
162 - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
1c4b9e2 @soffes Basic collection view working
soffes authored
163 _highlighted = highlighted;
f43ac45 @soffes Started on collection view
soffes authored
164
19937bf @soffes Implement `backgroundView` and `selectedBackgroundView` in SSCollecti…
soffes authored
165 void (^changes)(void) = ^{
166 for (UIView *view in [self subviews]) {
167 if ([view respondsToSelector:@selector(setHighlighted:)]) {
168 [(UIControl *)view setHighlighted:_highlighted];
169 }
c267557 @soffes Add support for highlighting and selecting to SSCollectionViewItem
soffes authored
170 }
19937bf @soffes Implement `backgroundView` and `selectedBackgroundView` in SSCollecti…
soffes authored
171 };
172
173 if (animated) {
174 [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:changes completion:nil];
175 } else {
176 changes();
177 }
c267557 @soffes Add support for highlighting and selecting to SSCollectionViewItem
soffes authored
178 }
179
180
181 #pragma mark Setters
182
183 - (void)setSelected:(BOOL)selected {
184 [self setSelected:selected animated:YES];
185 }
186
187
188 - (void)setHighlighted:(BOOL)selected {
189 [self setHighlighted:selected animated:YES];
f43ac45 @soffes Started on collection view
soffes authored
190 }
191
192 @end
Something went wrong with that request. Please try again.