This code is an idea on how to get the affected rectangles. For each new rectangle that touches the counter, 1 will be added, so itβs easier to track the movement of the touch (if necessary). For greater accuracy, change the values ββof the constants ROWS and COLS
#define ROWS 15 #define COLS ROWS int matrix[ROWS][COLS]; int squareCounter; - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint location = [touch locationInView:touch.view]; int col = floor((location.x * COLS) / self.view.frame.size.width); int row = floor((location.y * ROWS) / self.view.frame.size.height); if (matrix[col][row] == 0) matrix [col][row] = ++ squareCounter; [label setText:[NSString stringWithFormat:@"[%d][%d] -> %d", col, row, matrix[col][row]]]; } - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSString *matrixStr = @""; for (int i = 0; i < COLS; i++) { for (int j = 0; j < ROWS; j++) { matrixStr = [matrixStr stringByAppendingFormat:@"%d\t", matrix[j][i]]; } matrixStr = [matrixStr stringByAppendingString:@"\n"]; } NSLog(@"Matrix:\n%@", matrixStr); } - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { squareCounter = 0; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { matrix[i][j] = 0; } } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];
It must be placed on the view controller whose presentation you want to determine the shape of the touch.
source share