-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLolayGridViewCell.m
176 lines (146 loc) · 4.87 KB
/
LolayGridViewCell.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// Copyright 2012 Lolay, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "LolayGridViewCell.h"
#import "NSString+LolayUUID.h"
@interface LolayGridViewCell ()
@property (nonatomic, strong) NSString* uuid;
@property (nonatomic) NSInteger rowIndex;
@property (nonatomic) NSInteger columnIndex;
@property (nonatomic) BOOL highlightedValue;
@property (nonatomic, strong) UIView* backgroundView;
@end
@implementation LolayGridViewCell
@dynamic highlighted;
@synthesize reuseIdentifier = reuseIdentifier_;
@synthesize uuid = uuid_;
@synthesize rowIndex = rowIndex_;
@synthesize columnIndex = columnIndex_;
@synthesize highlightedValue = highlightedValue_;
@synthesize backgroundView = backgroundView_;
@synthesize delegate = delegate_;
@synthesize isHighlightable = isHighlightable_;
#pragma mark -
#pragma mark View Lifecycle
- (void) setupWithFrame:(CGRect) frame reuseIdentifier:(NSString*) reuseIdentifier {
self.uuid = [NSString stringWithUUID];
DLog(@"[LolayGridViewCell initWithFrame] uuid=%@", self.uuid);
self.reuseIdentifier = reuseIdentifier;
self.frame = frame;
self.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
self.backgroundView.backgroundColor = [UIColor blackColor];
self.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backgroundView.opaque = NO;
[self addSubview:self.backgroundView];
self.isHighlightable = YES;
[self prepareForReuse];
}
- (id) initWithFrame:(CGRect) inRect reuseIdentifier:(NSString*) inReuseIdentifier {
self = [super initWithFrame:inRect];
if (self) {
[self setupWithFrame:inRect reuseIdentifier:inReuseIdentifier];
}
return self;
}
- (id) initWithReuseIdentifier:(NSString*) inReuseIdentifier {
return [self initWithFrame:CGRectZero reuseIdentifier:inReuseIdentifier];
}
- (void) prepareForReuse {
self.highlighted = NO;
self.rowIndex = -1;
self.columnIndex = -1;
self.backgroundView.hidden = YES;
self.backgroundView.alpha = 0.0;
}
#pragma mark -
#pragma mark View Cell Methods
- (void) setRow:(NSInteger) gridRowIndex atColumn:(NSInteger) gridColumnIndex {
self.rowIndex = gridRowIndex;
self.columnIndex = gridColumnIndex;
}
- (void) setHighlighted:(BOOL) inHighlighted {
self.highlightedValue = inHighlighted;
self.backgroundView.alpha = inHighlighted ? 0.25 : 0.0;
self.backgroundView.hidden = ! inHighlighted;
}
- (BOOL) highlighted {
return self.highlightedValue;
}
- (void) setHighlighted:(BOOL) inHighlighted animated:(BOOL) inAnimated {
if (inAnimated) {
self.highlightedValue = inHighlighted;
self.backgroundView.hidden = NO;
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseOut animations:^{
if (inHighlighted) {
self.backgroundView.alpha = 0.25;
} else {
self.backgroundView.alpha = 0.0;
}
} completion:^(BOOL finished) {
if (! inHighlighted) {
self.backgroundView.hidden = YES;
}
}];
} else {
[self setHighlighted:inHighlighted];
}
}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*) event {
if (! self.highlighted && self.isHighlightable) {
[self setHighlighted:YES animated:NO];
}
[super touchesBegan:touches withEvent:event];
}
- (void) touchesCancelled:(NSSet*) touches withEvent:(UIEvent*)event {
if (self.highlighted) {
[self setHighlighted:NO animated:YES];
}
[super touchesCancelled:touches withEvent:event];
}
- (void) touchesEnded:(NSSet*) touches withEvent:(UIEvent*) event {
if (self.highlighted) {
[self setHighlighted:NO animated:YES];
if ([self.delegate respondsToSelector:@selector(didSelectGridCell:)]) {
[self.delegate didSelectGridCell:self];
}
} else if (!self.isHighlightable) {
if ([self.delegate respondsToSelector:@selector(didSelectGridCell:)]) {
[self.delegate didSelectGridCell:self];
}
}
[super touchesEnded:touches withEvent:event];
}
- (NSUInteger) hash {
return [self.uuid hash];
}
- (BOOL) isEqualToGridViewCell:(LolayGridViewCell*) other {
if (other == self) {
return YES;
}
if (! other || ! [other isKindOfClass:[self class]]) {
return NO;
}
return [self.uuid isEqualToString:other.uuid];
}
- (BOOL) isEqual:(id) other {
if (other == self) {
return YES;
}
if (! other || ! [other isKindOfClass:[self class]]) {
return NO;
}
return [self isEqualToGridViewCell:(LolayGridViewCell*) other];
}
@end