【发布时间】:2014-11-03 07:39:44
【问题描述】:
有没有办法在collectionViewCell 中获取点击点的坐标?如果我单击 Y 坐标 50,我想做方法 B。
【问题讨论】:
标签: ios iphone ipad uicollectionview cgpoint
有没有办法在collectionViewCell 中获取点击点的坐标?如果我单击 Y 坐标 50,我想做方法 B。
【问题讨论】:
标签: ios iphone ipad uicollectionview cgpoint
还有选项 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
然后从它自己的 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
}
}
【讨论】:
假设您有一个自定义UICollectionViewCell,您可以将UITapGestureRecognizer 添加到单元格并在 touchesBegan 处理程序中获取接触点。像这样:
//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];
}
}
【讨论】: