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

FIX for indexOfObject: returns max int for tapped card #5

Open
danipralea opened this issue Apr 4, 2014 · 0 comments
Open

FIX for indexOfObject: returns max int for tapped card #5

danipralea opened this issue Apr 4, 2014 · 0 comments

Comments

@danipralea
Copy link

Hi, you might want to replace the code for - (void)handleTap:(UIGestureRecognizer *)gestureRecognizer

with

    if ([self.gridViewDelegate respondsToSelector:@selector(gridView:didSelectCell:index:)]) {
        CGRect viewFrame = gestureRecognizer.view.frame;
        NSInteger index2 = [self.gridRects indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
            NSValue \* value = (NSValue *)obj;
            CGRect rect = [value CGRectValue];
            CGFloat lowX = viewFrame.origin.x - 0.1;
            CGFloat highX = viewFrame.origin.x + 0.1;

    CGFloat lowY = viewFrame.origin.y - 0.1;
    CGFloat highY = viewFrame.origin.y + 0.1;

    CGFloat lowWidth = viewFrame.size.width - 0.1;
    CGFloat highWidth = viewFrame.size.width + 0.1;

    CGFloat lowHeight = viewFrame.size.height - 0.1;
    CGFloat highHeight = viewFrame.size.height + 0.1;

    BOOL xOK = rect.origin.x >= lowX && rect.origin.x <= highX;
    BOOL yOK = rect.origin.y >= lowY && rect.origin.y <= highY;
    BOOL widthOK = rect.size.width >= lowWidth && rect.size.width <= highWidth;
    BOOL heightOK = rect.size.height >= lowHeight && rect.size.height <= highHeight;

    return (xOK && yOK && widthOK && heightOK);
}];
[self.gridViewDelegate gridView:self didSelectCell:gestureRecognizer.view index:index2];

}


}```

because there are cases when values for rectangles do no match (because of the floating point) the frame of the tapped view.
Example : you have in your array one rect with origin.x = 56.666444, but you tapped view has origin.x = 56.666452, so it will give you out of bounds index for that card, resulting in crashing the application.
That's why you don't search a rectangle in an array of rectangles.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant