【问题标题】:How to find location of the view being tap (using the GMGridView library)如何找到被点击的视图的位置(使用 GMGridView 库)
【发布时间】:2012-09-09 10:26:32
【问题描述】:

我已经在我的程序中实现了 GMGridview。我在 github 上找到了这段代码。点击here 我的程序是我的业务产品的网格视图。每个产品都是滚动视图中的自定义 UIButton。我正在寻找获取每个按钮(即产品)位置的方法,但是每次单击不同的按钮时,它仍然给我相同的位置。我不明白这是为什么。它应该检测我正在单击的按钮。

我使用此代码获取位置:

 CGPoint myPoint = CGPointMake (senderButton.frame.origin.x, senderButton.frame.origin.y); 
 CGPoint angelPoint= [senderButton.superview convertPoint:myPoint toView:self.view];

我也研究了这个问题的一些解决方案,但在这种情况下它对我不起作用。

谢谢。

【问题讨论】:

  • 您是否只想找到您点击了哪个按钮或点击在按钮中的位置?
  • 我需要找到我点击的按钮的位置。即使我点击了不同的按钮,我的日志中的位置也保持不变。

标签: objective-c xcode ipad uibutton gmgridview


【解决方案1】:

以网格方式创建按钮并为每个按钮设置标签:

这里只有 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;
    }

然后从视图中提取每个按钮及其标签:

    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...
    }

【讨论】:

  • 我正在重用按钮,就像在表格视图中一样。我不认为这些标签对我有用。另外,我需要得到具体的坐标。我看不出这些标签对我有什么帮助。不过还是谢谢回答!无论我点击什么按钮,它都会给我一个恒定的坐标。
  • 你想使用标签。为什么不更改标签,因为按钮被重用?您必须在重用时更改其他属性。
【解决方案2】:

通常,不需要对点击事件的坐标进行计算即可知道您点击了哪个按钮。事实上,senderButton 参数正是在告诉您这一点。您可能会考虑在构建网格时将tag 值关联到每个按钮(例如,序列号),然后在您的操作处理程序中使用该标记来识别更“合乎逻辑”级别的按钮:

至于您的原始问题,您的代码似乎很好。我看到的唯一潜在问题是self.view。它确定了哪个视图?您是否尝试过传递nil 以便获得UIWindow 空间中的坐标?

【讨论】:

  • 嗨。我将它设置为 nil 但它也不起作用。无论我点击什么按钮,它仍然给我相同的坐标。
  • 您会不会:NSLog(@"Button Info: %@", [senderButton description]); 并确保每次传入不同的按钮?
【解决方案3】:

您可以在 GMGridView 中编辑tapGestureUpdated 方法,以便它将您的点击位置(在您的按钮坐标中)传递给您的委托方法:

GMGridView.h:

#pragma mark Protocol SCLGridViewActionDelegate
@protocol SCLGridViewActionDelegate <NSObject>

@required
- (void)GMGridView:(SCLGridView *)gridView didTapOnItemAtIndex:(NSInteger)index atLocation: (CGPoint) location;
@end

GMGridView.m:

#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];
       }
}

编辑

如果您的 gridView 是由视图控制器管理的视图的一部分,请确保您的视图控制器符合 SCLGridViewActionDelegate 并将您的视图控制器设置为操作委托:yourGridView.actionDelegate = self where self是您的视图控制器。然后在您的视图控制器中实现didTapInItemAtIndex 方法。

【讨论】:

  • 嗨。感谢您的代码。但它对我不起作用。我在我的程序上试过了,但是当我点击产品时,函数本身没有被调用。
  • 你通过你的代码调试了吗? didTapOnItemAtIndex 是一个委托方法,您需要使您的类符合 SCLGridViewActionDelegate 协议,您还需要设置您的操作委托。我一直在我的代码中使用它真的没有理由不应该工作。
  • 请注意,我在被点击的视图中传递了点击的位置。如果你想获得相对于超级视图的位置,只需将 locationTouch 而不是 locationInItem 传递给 [self.actionDelegate GMGridView:self didTapOnItemAtIndex:index atLocation:locationInItem];方法
  • 太棒了!我想你在这里很新,但如果这是正确的,请接受/支持答案。如果您有疑问,请查看常见问题解答:stackoverflow.com/faq
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多