I am making a grid view. It subclasses tableview and lays out multiple subcells (开发者_运维问答columns) per cell. That's all working fine.
Now, I need to detect when an individual sub-cell is tapped. I have overriden touchesEnded in the grid view. Is there a way I can take that NSSet
of UITouch
objects and detect whether it was a touch up inside or some other gesture?
I could write custom code, but it might be hard to get it perfect.
UIControlEvents
like UIControlEventTouchUpInside
are used with UIControl
objects. In a UIView
you'll need to do your own testing.
Now I don't know how you've structured your grid view, or exactly what kind of touches you want to detect, but normally you need to have something like this. This looks for a single tap and what cell was tapped.
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] == 1) {
UITouch * touch = [touches anyObject];
if ([touch tapCount] == 1) {
// This is a simple tap
CGPoint point = [touch locationInView:self.view];
GridCell * cell = nil;
for (GridCell * aCell in cells) {
if (CGRectContainsPoint(aCell.frame, point)) {
cell = aCell;
break;
}
}
if (cell) {
// The tap was inside this cell
}
}
}
}
Assuming you are only supporting single-touch in the view, touchesEnded
means that the user removed his or her finger from the view. Whether the touch is inside or outside the view or a specific part of the view is something that you should test for.
From my experience, you need to create a Custom View for each cell instead of subclassing the TableView to get the events. With this, you have the full control on all the events of your interest. Then it's your choice to lay them out in the screen, it's not very hard to implement.
But let's look at others' answers if there are any ;)
精彩评论