Skip to content

Commit

Permalink
Better tile touch selection semantics (matches MobileCal behavior)
Browse files Browse the repository at this point in the history
  • Loading branch information
klazuka committed Dec 21, 2009
1 parent 40dfc7b commit f0aad7c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
2 changes: 2 additions & 0 deletions Kal/KalGridView.h
Expand Up @@ -26,11 +26,13 @@
id<KalViewDelegate> delegate; // Assigned.
KalLogic *logic;
KalTileView *selectedTile;
KalTileView *highlightedTile;
NSMutableArray *reusableCells; // The pool of reusable cells. If this runs out, the app will crash instead of dynamically allocating more views. So make this just large enough to meet your app needs, but no larger.
CGFloat cellHeight; // Every cell must have the same height: specifically, the height stored here.
}

@property (nonatomic, retain) KalTileView *selectedTile;
@property (nonatomic, retain) KalTileView *highlightedTile;

- (id)initWithFrame:(CGRect)frame logic:(KalLogic *)logic delegate:(id<KalViewDelegate>)delegate;

Expand Down
45 changes: 21 additions & 24 deletions Kal/KalGridView.m
Expand Up @@ -25,7 +25,7 @@ - (void)selectTodayIfVisible;

@implementation KalGridView

@synthesize selectedTile;
@synthesize selectedTile, highlightedTile;

- (id)initWithFrame:(CGRect)frame logic:(KalLogic *)theLogic delegate:(id<KalViewDelegate>)theDelegate
{
Expand Down Expand Up @@ -80,16 +80,6 @@ - (void)refresh
}
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
UIView *hitView = [self hitTest:location withEvent:event];

if ([hitView isKindOfClass:[KalTileView class]])
self.selectedTile = (KalTileView*)hitView;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
Expand All @@ -99,8 +89,15 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
if (!hitView)
return;

if ([hitView isKindOfClass:[KalTileView class]])
self.selectedTile = (KalTileView*)hitView;
if ([hitView isKindOfClass:[KalTileView class]]) {
KalTileView *tile = (KalTileView*)hitView;
if (tile.belongsToAdjacentMonth) {
self.highlightedTile = tile;
} else {
self.highlightedTile = nil;
self.selectedTile = tile;
}
}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
Expand All @@ -118,16 +115,9 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
[delegate showPreviousMonth];
}
}

self.selectedTile = tile;
}
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
NSLog(@"cancelled touches at %@", NSStringFromCGPoint([touch locationInView:self]));
self.selectedTile = nil;
self.highlightedTile = nil;
}

- (void)updateTilesInKeptCell:(UIView*)cell
Expand All @@ -150,6 +140,15 @@ - (void)sizeToFit
self.height = [[self.subviews valueForKeyPath:@"@sum.height"] floatValue];
}

- (void)setHighlightedTile:(KalTileView *)tile
{
if (highlightedTile != tile) {
highlightedTile.highlighted = NO;
highlightedTile = [tile retain];
tile.highlighted = YES;
}
}

- (void)setSelectedTile:(KalTileView *)tile
{
if (selectedTile != tile) {
Expand Down Expand Up @@ -345,13 +344,11 @@ - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished c
- (void)dealloc
{
[selectedTile release];
[highlightedTile release];
[reusableCells release];
[logic release];
[super dealloc];
}


@end



8 changes: 7 additions & 1 deletion Kal/KalTileView.m
Expand Up @@ -96,6 +96,12 @@ - (void)setSelected:(BOOL)selected
[self reloadStyle];
}

- (void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
[self reloadStyle];
}

#pragma mark UIView and UIResponder

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[self nextResponder] touchesBegan:touches withEvent:event]; }
Expand Down Expand Up @@ -154,7 +160,7 @@ - (void)reloadStyle
dayLabel.shadowColor = nil;
backgroundView.image = [UIImage imageNamed:@"kal_tile.png"];
markerImage = [UIImage imageNamed:@"kal_marker_disabled.png"];
if (self.selected) {
if (self.highlighted) {
self.backgroundColor = [UIColor lightGrayColor];
}
break;
Expand Down

0 comments on commit f0aad7c

Please sign in to comment.