在CollectionView的didSelectItemAtIndexPath方法中获取CGPoint

Ale*_*rNS 3 iphone ipad cgpoint ios uicollectionview

有没有办法可以在collectionViewCell中获得点击点的坐标?如果我点击Y坐标<50的rect,我想做方法A,如果Y> 50,我想做方法B.

Lef*_*ris 9

还有选项B,用于子类化UITableViewCell并从UIResponder类中获取位置:

@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
Run Code Online (Sandbox Code Playgroud)

然后从TableViewCell获取自己的位置:

- (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
    }
}
Run Code Online (Sandbox Code Playgroud)