3

Is there a way that I can get coordinates of clicked point inside collectionViewCell? I want to do method A if I clicked at rect with Y coordinate < 50, and method B if Y >50.

AleksandarNS
  • 265
  • 1
  • 2
  • 15

2 Answers2

9

There is also option B, to subclass the UITableViewCell and get the location from the UIResponder class:

@interface CustomTableViewCell : UITableViewCell

@property (nonatomic) CGPoint clickedLocation;

@end

@implementation CustomTableViewCell

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    self.clickedLocation = [touch locationInView:touch.view];
}

@end

Then get the location from the TableViewCell it self:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //get the cell
    CustomTableViewCell *cell = (CustomTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    //get where the user clicked
    if (cell.clickedLocation.Y<50) {
        //Method A
    }
    else {
        //Method B
    }
}
Lefteris
  • 14,550
  • 2
  • 56
  • 95
1

Assuming you have a custom UICollectionViewCell, you can add a UITapGestureRecognizer to the cell and get the touch point in the touchesBegan handler. Like this:

 //add gesture recognizer to cell
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] init];
[cell addGestureRecognizer:singleTap];

//handler
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   UITouch *touch = [touches anyObject];
   CGPoint touchPoint = [touch locationInView:self.view];

   if (touchPoint.y <= 50) {
      [self methodA];
   }
   else {
      [self methodB];
   }
}
instaable
  • 3,449
  • 2
  • 24
  • 31