Objective c 如何找到正在点击的视图的位置(使用GMGridView库)
我已经在我的程序中实现了GMGridview。我在github上找到了这段代码。点击 我的程序是我公司产品的网格视图。每个产品都是滚动视图中的自定义UI按钮。我正在寻找方法来获得每个按钮(即产品)的位置,但每次我单击不同的按钮,它仍然会给我相同的位置。我不明白这是为什么。它应该能检测到我点击的按钮 我使用此代码获取位置:Objective c 如何找到正在点击的视图的位置(使用GMGridView库),objective-c,xcode,ipad,uibutton,gmgridview,Objective C,Xcode,Ipad,Uibutton,Gmgridview,我已经在我的程序中实现了GMGridview。我在github上找到了这段代码。点击 我的程序是我公司产品的网格视图。每个产品都是滚动视图中的自定义UI按钮。我正在寻找方法来获得每个按钮(即产品)的位置,但每次我单击不同的按钮,它仍然会给我相同的位置。我不明白这是为什么。它应该能检测到我点击的按钮 我使用此代码获取位置: CGPoint myPoint = CGPointMake (senderButton.frame.origin.x, senderButton.frame.origin.y
CGPoint myPoint = CGPointMake (senderButton.frame.origin.x, senderButton.frame.origin.y);
CGPoint angelPoint= [senderButton.superview convertPoint:myPoint toView:self.view];
我也研究了一些解决这个问题的方法,但在这种情况下,它对我不起作用
谢谢。通常,不需要计算点击事件的坐标,就可以知道您点击了哪个按钮。实际上,
senderButton
参数正好告诉您这一点。在构建网格时,您可能会考虑将一个值与每个按钮关联(例如,序列号),然后在操作处理程序中使用该标记在更“逻辑”的级别上标识按钮:
至于您最初的问题,您的代码似乎很好。我看到的唯一潜在问题是
self.view
。它确定了哪种观点?您是否尝试过传递nil
,以便在ui窗口
空间中获得坐标?以网格方式创建按钮,并为每个按钮设置标签:
这里只有4个按钮
CGFloat xPoint = 0.0;
for (int t = 0; t < 4; t ++) {
UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(xPoint,0.0,100.0,50.0)];
[button setTag:t];
[button addTarget:self action:@selector('your Selector')forControlEvents:UIControlEventTouchUpInside];
[YOURVIEW addSubview:button];
xPoint += 100.0;
}
CGFloat xPoint=0.0;
for(int t=0;t<4;t++){
UIButton*button=[[UIButton alloc]initWithFrame:CGRectMake(xPoint,0.0100.0,50.0)];
[按钮设置标签:t];
[按钮添加目标:自我操作:@selector(“您的选择器”)for ControlEvents:UIControlEventTouchUpInside];
[您的视图添加子视图:按钮];
xPoint+=100.0;
}
然后从视图中提取每个按钮及其标记:
for(int j = 0;j < 4;j++)
{
UIButton * removeButton = (UIButton *)[self.view viewWithTag:j];
NSLog(@"Frame : X:%.2f Y:%.2f Width:%.2f Height:%.2f",removeButton.frame.origin.x,removeButton.frame.origin.x,removeButton.frame.size.width,removeButton.frame.size.height);
// you can get access each button's frame here...
}
for(int j=0;j<4;j++)
{
UIButton*removeButton=(UIButton*)[self.view视图带标记:j];
NSLog(@“帧:X:%.2f Y:%.2f宽度:%.2f高度:%.2f”,removeButton.Frame.origin.X,removeButton.Frame.origin.X,removeButton.Frame.size.Width,removeButton.Frame.size.Height);
//你可以在这里访问每个按钮的框架。。。
}
您可以在GMGridView中编辑tapGestureUpdated
方法,以便它将沿着您的点击位置(在按钮坐标中)传递给您的代理方法:
GMGridView.h:
#pragma mark Protocol SCLGridViewActionDelegate
@protocol SCLGridViewActionDelegate <NSObject>
@required
- (void)GMGridView:(SCLGridView *)gridView didTapOnItemAtIndex:(NSInteger)index atLocation: (CGPoint) location;
@end
编辑
如果您的gridView是由视图控制器管理的视图的一部分,请确保视图控制器符合
SCLGridViewActionDelegate
,并将视图控制器设置为动作委托:yourGridView.actionDelegate=self
其中self
是您的视图控制器。然后在视图控制器中实现了didTapInItemAtIndex
方法。是否只想找到您点击的按钮或点击在按钮中的位置?我需要找到我点击的按钮的位置。即使我点击了不同的按钮,我在日志中也会得到一个固定的位置。我像在表视图中一样重用这些按钮。我认为这些标签对我不合适。另外,我需要得到具体的坐标。我看不出这些标签对我有什么帮助。谢谢你的回答!无论我点击什么按钮,它都会给我一个恒定的坐标。你想使用标签。为什么不在重复使用按钮时更改标记?您必须在reuse.Hi上更改其他属性。谢谢你的密码。但这对我不起作用。我在我的程序上尝试了它,但是当我点击产品时,函数本身没有被调用。你通过你的代码调试了吗?DIDTAPonimatIndex是一种委托方法,您需要使类符合SCLGridViewActionDelegate协议,还需要设置动作委托。我在我的代码中一直在使用它。请注意,我正在传递正在点击的视图中点击的位置。如果要获取相对于超级视图的位置,只需将locationTouch而不是locationInItem传递给[self.actionDelegate GMGridView:self-didtaponitemIndex:index-atLocation:locationInItem];太好了!我猜你是新来的,但如果答案正确,请接受/投票。如果您有疑问,请查看常见问题解答:。您好。我把它设为零,但它也不起作用。无论我点击哪个按钮,它都会给我相同的坐标。你会怎么做:NSLog(@“按钮信息:%@,[senderButton description])
并确保每次传入不同的按钮时?
#pragma mark tapgesture
- (void)tapGestureUpdated:(UITapGestureRecognizer *)tapGesture
{
CGPoint locationTouch = [_tapGesture locationInView:self];
NSInteger index = [self.layoutStrategy itemPositionFromLocation:locationTouch];
if (index != kInvalidPosition)
{
CGPoint locationInItem = [_tapGesture locationInView:[self cellForItemAtIndex:index]];
[self.actionDelegate GMGridView:self didTapOnItemAtIndex:index atLocation:locationInItem];
}
}