I’m practically new to Objective C and iOS Dev and I need to make an app that records a “route” or “path” (like the dot lock protection app). So I thought in buttons pressed while dragging, enabling all the direct neighbors (right, left, up, down). I have started coding but I dont know if I’m in the right direction and How can I implement a dragging press, so that the user don’t have to press each button, but trace a path with it’s finger
viewController.m
(void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
for (int y=0; y < 9; y++) {
for (int x = 0; x < 9; x++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(20 + 30 * x, 20 + 30 * y, 30, 30);
unsigned buttonNumber = y * 9 + x + 1;
button.tag = buttonNumber;
button.backgroundColor = [UIColor blackColor];
[button setTitle:[NSString stringWithFormat:@"%u", buttonNumber] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:@"!"] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview: button];
}
}
This code (adapted from Jorge’s in my other post) generates a 9×9 button grid with unique tag.
And the respective action method,
(void)buttonPressed:(UIButton *)button
if(first){
for (int y=0; y < 9; y++) {
for (int x = 0; x < 9; x++) {
unsigned buttonNumber = y * 9 + x + 1;
UIButton *auxButton = (UIButton *)[self.view viewWithTag:buttonNumber];
if ((auxButton.tag != (button.tag + 1)) || (auxButton.tag != (button.tag - 1)) || (buttonNumber != (button.tag + 9)) || (buttonNumber != (button.tag - 9)) || (buttonNumber != button.tag )){
auxButton.enabled = FALSE;
}
}
}else{
//not implemented yet
}
What I did here was: initially all buttons are enabled, but when the first one is pressed it will disable all the buttons except for the direct neighbors (l,r,u,d).
My if(condition) is not working with OR conditions (||), so all buttons are disabled, but if I only use one condition, say if(auxButton.tag != button.tag + 1), it works.
What could be wrong in here?
How can I implement that a path traced with the touch presses all the buttons it passes by?
Thanks in advance!
To disable all the buttons except
buttonand its neighbors you can do the following:Remove the test
(other != button)if you want button to be disabled too.Also check if you want use
UIControlEventTouchDragInsideorUIControlEventTouchDragEnterinstead ofUIControlEventTouchUpInside. See documentation here.