forked from soffes/sstoolkit
/
SSCollectionViewItem.m
142 lines (103 loc) · 3.37 KB
/
SSCollectionViewItem.m
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
137
138
139
140
141
142
//
// SSCollectionViewItem.m
// SSToolkit
//
// Created by Sam Soffes on 6/11/10.
// Copyright 2010-2011 Sam Soffes. All rights reserved.
//
#import "SSCollectionViewItem.h"
#import "SSCollectionViewItemInternal.h"
#import "SSCollectionView.h"
#import "SSCollectionViewInternal.h"
#import "SSLabel.h"
#import "SSDrawingUtilities.h"
@implementation SSCollectionViewItem
@synthesize imageView = _imageView;
@synthesize textLabel = _textLabel;
@synthesize detailTextLabel = _detailTextLabel;
@synthesize backgroundView = _backgroundView;
@synthesize selectedBackgroundView = _selectedBackgroundView;
@synthesize reuseIdentifier = _reuseIdentifier;
@synthesize selected = _selected;
@synthesize highlighted = _highlighted;
@synthesize indexPath = _indexPath;
@synthesize collectionView = _collectionView;
#pragma mark NSObject
- (void)dealloc {
self.collectionView = nil;
[_indexPath release];
[_imageView release];
[_textLabel release];
[_detailTextLabel release];
[_backgroundView release];
[_selectedBackgroundView release];
[_reuseIdentifier release];
[super dealloc];
}
#pragma mark UIResponder
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self setHighlighted:YES animated:NO];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self setHighlighted:NO animated:NO];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self setHighlighted:YES animated:NO];
if (CGRectContainsPoint(CGRectSetZeroOrigin(self.frame), [[touches anyObject] locationInView:self])) {
[self.collectionView selectItemAtIndexPath:self.indexPath animated:YES];
}
}
#pragma mark UIView
- (id)initWithFrame:(CGRect)frame {
return [self initWithStyle:SSCollectionViewItemStyleDefault reuseIdentifier:nil];
}
- (void)layoutSubviews {
if (_style == SSCollectionViewItemStyleImage) {
_imageView.frame = CGRectSetZeroOrigin(self.frame);
}
}
#pragma mark SSCollectionViewItem
- (id)initWithStyle:(SSCollectionViewItemStyle)style reuseIdentifier:(NSString *)aReuseIdentifier {
if ((self = [super initWithFrame:CGRectZero])) {
_style = style;
_reuseIdentifier = [aReuseIdentifier copy];
if (_style != SSCollectionViewItemStyleBlank) {
_imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
[self addSubview:_imageView];
_textLabel = [[SSLabel alloc] initWithFrame:CGRectZero];
_textLabel.textAlignment = UITextAlignmentCenter;
[self addSubview:_textLabel];
_detailTextLabel = [[SSLabel alloc] initWithFrame:CGRectZero];
_detailTextLabel.textAlignment = UITextAlignmentCenter;
[self addSubview:_detailTextLabel];
}
}
return self;
}
- (void)prepareForReuse {
// Do nothing. Subclasses can override this
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
_selected = selected;
for (UIView *view in [self subviews]) {
if ([view respondsToSelector:@selector(setSelected:)]) {
[(UIControl *)view setSelected:selected];
}
}
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
_highlighted = highlighted;
for (UIView *view in [self subviews]) {
if ([view respondsToSelector:@selector(setHighlighted:)]) {
[(UIControl *)view setHighlighted:highlighted];
}
}
}
#pragma mark Setters
- (void)setSelected:(BOOL)selected {
[self setSelected:selected animated:YES];
}
- (void)setHighlighted:(BOOL)selected {
[self setHighlighted:selected animated:YES];
}
@end