0

I have many buttons in the view and 1 method with a different parameter for all of that buttons (when touched down)

Now I want to slide from a button to others that this method can be triggered for each button. For example I slide from button 1 to button 5, the method can be triggered 5 times with parameter from 1 to 5.

I have many buttons, so I can't use touchesMoved.

These are the buttons:

Zombo
  • 1
  • 62
  • 391
  • 407
erictruong
  • 303
  • 3
  • 14

2 Answers2

0

First create your button list as a instance variable:

NSMutableArray *_buttonList;

in viewDidLoad:

_buttonList  = [[NSMutableArray alloc] init];

Then:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_buttonList removeAllObjects];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = touches.anyObject;
    UIView *touchView = touch.view;

    if ([touchView isKindOfClass:[UIButton class]]
        && ! [_buttonList containsObject:touchView])
    { // add any additionnal check you may want
        [_buttonList addObject:touchView];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    for (UIButton *button in _buttonList) {
        [button sendActionsForControlEvents:UIControlEventTouchUpInside];
    }
}
The Windwaker
  • 1,054
  • 12
  • 24
  • It may be incorrect code but I hope you get the thing – The Windwaker Aug 10 '15 at 08:51
  • Now if you want this behavior only if you start from one of your `UIButtons`, tell me and I will change my code – The Windwaker Aug 10 '15 at 08:53
  • When i touch button, these methods don't run. They only run when i touch the view contain buttons. – erictruong Aug 10 '15 at 08:58
  • Try to set the target of your button to act on UIControlEventTouchUpInside – The Windwaker Aug 10 '15 at 09:00
  • I found a solution for this http://stackoverflow.com/questions/631427/is-there-a-way-to-pass-touches-through-on-the-iphone – The Windwaker Aug 10 '15 at 09:10
  • It doesn't work :/ Can you see my image in the question i've edited. – erictruong Aug 10 '15 at 09:10
  • Yes I can see it. And yes, without the code in my last comment, it doesn't work. Use the code in my last comment for touchesBegan, touchesMoved and touchesEnded in a category or subclass and let me know if it works. If yes I will add the link in my reply before validating. – The Windwaker Aug 10 '15 at 09:13
  • and I just understood what you are looking for. You want the event to be triggered by the button as soon as the touch enters it ? – The Windwaker Aug 10 '15 at 09:17
  • i found this: http://stackoverflow.com/questions/8195420/slide-to-and-from-uibuttons-and-make-them-respond But i don't understand what it works, do you know? – erictruong Aug 10 '15 at 09:25
  • I'm not familiar with this. But in my opinion, if you do a piano like app, the better thing might be to handle the touch yourself. Using simple UIViews and dealing with UIGestureRecognizers or simply with touches methods like - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ – The Windwaker Aug 10 '15 at 11:38
0

Add buttons in a view and add tags to all better in increasing order then(You can map array entry with a button) add add panGestureRecognizer in that view. On handler method check panGesture state if its began or ended. And then get location of pan. Now you can loop over buttons and check which button frame lies at that position and you will know which button is at that position.

You can do this for both states to get start and end button, loop from startButtonTag to endButtonTag and UIButton *button = (UIButton*)[view viewWithTag:tag] and then can call method by sending that button as parameter.

Adnan Aftab
  • 14,377
  • 4
  • 45
  • 54