How to find location of the view being tap (using the GMGridView library)

736 Views Asked by At

I've implemented the GMGridview in my program. I found this code on github. Click here My program is a grid view of my business' products. Each product is a custom UIButton inside a scroll view. Im looking for ways to get the location of each button (which is the product) but every time i click different buttons it still gives me the same location. I don't understand why this is. It supposed to detect what button I am clicking.

I used this code to get the location:

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

I've also researched some solutions to this problem but it didn't work for me, in this case.

Thank you.

3

There are 3 best solutions below

2
On

Create button in grid manner and setTag for each of them:

here it is only 4 button

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

Then extract each button with it's tag from the View:

    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
On

Usually, it is not required to do calculations on the coordinates of your tap event to know which button you have tapped. Indeed, the senderButton argument is just telling you exactly that. You might think of associating a tag value to each button when you build your grid (e.g, a sequence number), and then use that tag in your action handler to identify the button on a more "logical" level:

As to your original question, your code seems fine. The only potential issue I see is with self.view. Which view does it identify? And have you tried passing nil so that you get coordinates in the UIWindow space?

4
On

You can edit the tapGestureUpdated method within GMGridView so that it will pass along the location of your tap (in your button's coordinates) to your delegate method:

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

EDIT

If you are in the scenario where your gridView is part of a view managed by a view controller make sure your view controller conforms to the SCLGridViewActionDelegate and set your view controller as the action delegate: yourGridView.actionDelegate = self where self is your view controller. Have then implemented the didTapInItemAtIndex method implemented within your view controller.