I have a custom UIView that generates a set of subviews and display them in rows and columns like tiles. What I am trying to achieve is to allow the user to touch the screen and as the finger move, the tiles beneath it disappears.
The code below is the custom UIView that contains the tiles:
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
int i, j;
int maxCol = floor(self.frame.size.width/TILE_SPACING);
int maxRow = floor(self.frame.size.height/TILE_SPACING);
CGRect frame = CGRectMake(0, 0, TILE_WIDTH, TILE_HEIGHT);
UIView *tile;
for (i = 0; i<maxCol; i++) {
for (j = 0; j < maxRow; j++) {
frame.origin.x = i * (TILE_SPACING) + TILE_PADDING;
frame.origin.y = j * (TILE_SPACING) + TILE_PADDING;
tile = [[UIView alloc] initWithFrame:frame];
[self addSubview:tile];
[tile release];
}
}
}
return self;
}
- (void)touchesBegan: (NSSet *)touches withEvent:(UIEvent *)event {
UIView *tile = [self hitTest:[[touches anyObject] locationInView:self] withEvent:nil];
if (tile != self)
[tile setHidden:YES];
}
- (void)touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event {
UIView *tile = [self hitTest:[[touches anyObject] locationInView:self] withEvent:nil];
if (tile != self)
[tile setHidden:YES];
}
This approach works but however if the tiles get denser (i.e. small tiles and more tiles on the screen). The iPhone is less responsive as the finger move. It may be the hitTest taking a toll on the processor as it struggles to keep up but would like some opinions.
My questions are:
Is this an efficient way / right way to implement the touchesMoved?
If it isn't, what would be the recommended approach?
I tried moving the functionality into a custom Tile class (a sub UIView) which the class above would create and add as a subview. This subview Tile can handle the TouchesBegan but as the finger move, the other tiles does not receive the TouchesBegan even since the touches are still part of the initial touch sequence. Is there a way to implement it through the subview Tile class, how do other tiles receive the TouchesBegan/TouchesMoved event as the finger move?