Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a UIButton to GMGridViewCell's contentView #68

Open
flexaddicted opened this issue Apr 3, 2012 · 12 comments
Open

Adding a UIButton to GMGridViewCell's contentView #68

flexaddicted opened this issue Apr 3, 2012 · 12 comments
Labels

Comments

@flexaddicted
Copy link

First of all, thank you for sharing this fantastic component. It's very cool.

Now, I'm facing a little problem and I don't know to fix it.

I've created a custom UIView called CustomView. An instance of this view is assigned to the contentView property of GMGridViewCell class within cellForItemAtIndex method.

if(!cell) {

    // create CustomView here...
    cell.contentView = customView;
}

The custom view has a UIButton as a subview. The problem is that the button seems to not respond to tap events since they are redirected to GMGridView.

Is it possible to overcome this problem? Thank you in advance.

@mwyman
Copy link

mwyman commented Apr 12, 2012

This as a problem with the gesture recognizers detecting taps gobbling presses on cell views.

I've gotten around this problem by modifying the GMGridView's -gestureRecognizer:shouldRecieveTouch: to return false for either the tap or long-press gestures when the touched view is a subclass of UIControl or the descendent of one.

@flexaddicted
Copy link
Author

@mwyman Thank you very much for your reply. Do I have to use the gesture recognizer's view property and check if it's an UIControl class? Could you provide a simple snippet to reach the goal? Thank you.

@flexaddicted
Copy link
Author

I figured out a way to do it. For the sake of completeness I post the code I'm using. Maybe someone could be interested in.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if(gestureRecognizer == _tapGesture) {

        UIView* touchedView = [touch view];
        if([touchedView isKindOfClass:[UIButton class]]) {

            return NO;
        }
    }

    return YES;
}

@mwyman
Copy link

mwyman commented Apr 13, 2012

That should work in most cases. However, you should also test against the _sortingLongPressGesture, and you may want to check whether the touched view is a descendent of a UIButton (actually, I'd generalize to UIControl).

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ( gestureRecognizer == _tapGesture || gestureRecognizer == _sortingLongPressGesture ) {
        if ( [touch.view isDescendantOfView:self] ) {
            // Test if the touched view is a subview of a control
            for ( UIView *view = touch.view ; view != self ; view = view.superview )
                if ( [view isKindOfClass:[UIControl class]] )
                    return NO;
        }
    }

    return YES;
}

@DioNNiS
Copy link

DioNNiS commented Apr 17, 2012

mwyman, nice solution!

@ghost
Copy link

ghost commented Apr 19, 2012

Hey flexaddicted, I've tried adding a UIButton as a subview of my custom view but I can't get it to appear. Any idea what's missing?

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    view.backgroundColor = [UIColor greenColor];
    view.layer.masksToBounds = NO;
    view.layer.cornerRadius = 15;

    UIButton *plainButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [plainButton setImage:[UIImage imageNamed:@"Itsy1.jpg"] forState:UIControlStateNormal];
    [plainButton setFrame:CGRectMake(0, 0, 50, 50)];
    [plainButton setTitle:@"Button" forState:UIControlStateNormal];
    [view addSubview:plainButton];
    [view bringSubviewToFront:plainButton];

    cell.contentView = view;

@mrevoir
Copy link

mrevoir commented May 17, 2012

flexaddicted, how did you go about knowing which position in the grid view was being clicked?

@Ricardo1980
Copy link

Do I have to modify your source code to add a button? If I use a XIB, do I also have to modify the grid source code? Thanks.

@flexaddicted
Copy link
Author

@DigiCub Seems ok to me. I don't know what the problem could be.

@mrevoir Could you explain what do you mean?

@Ricardo1980 You need to create a view with its button and add it as the contentView of the cell. Yes, you have to modify the code also with a xib.

@mrevoir
Copy link

mrevoir commented May 18, 2012

@flexaddicted In thinking about how to better describe my question it struck me to look at how cell deletion currently works so I'll do that as well, but I am wondering how to determine which cell in the grid contained the button that was clicked. I can see that the button is receiving the event because my selector is invoked, but I don't know which of the buttons in the grid was clicked.

@mwyman
Copy link

mwyman commented May 19, 2012

One method would be to use setTag: on your buttons when you get the cell in GMGridView:cellForItemAtIndex:, setting it to the index of the cell. Note that you will need to do this every time you recycle a cell, not just when you create the buttons.

@exalted
Copy link

exalted commented Jun 21, 2012

Thanks! I would love to see this merged on the master though.

ricsantos pushed a commit to NextFaze/GMGridView that referenced this issue Dec 12, 2013
… events. Added in (slightly modified) fix as suggested by @mwyman to ignore tap gestures that land on instances of UIControl.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants