views:

439

answers:

2

Hi,

I have 4-5 uibuttons(also can use uiview instead of button) side by side next to each other.

When user moves touch from one button to other(outside of self view), i want to cancel this touch and start touch for the next button(view).

How to achieve this functionality?

I used uiview instead of buttons kept it next to each other, handled its touches events.

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

UITouch *touch = [touches anyObject]; //anyobject
CGPoint touchLocation = [touch locationInView:touch.view];
if(![self pointInside:touchLocation withEvent:nil])
{
 //touch is outside of my current view , do something to cancel current touch and i can get touchesmoved for the next view . help me to write something.
}

}

Is there any other way to handle event when we have such UI.

Thanks.

+1  A: 

If you are using UIButtons, you can just use the button's events to do this. When you get a touchDown or touchDragInside, then play your note. Stop when you get a touchUpInside or a touchDragOutside.

mahboudz
Thanks fir the help. I tried with UIButton's events. touchdown wil work perfectly fine. but drag events(dragoutside and draginside) location is way outside the button.
Niraj
Do you have any working code that can help me to solve this?
Niraj
The way drag events for UIButtons works is that it looks to see where the drag started and where it ended. If it started outside a button and got inside the button's area, then it calls your method (IBAction). If it started inside the button and ended outside, then it calls your touchDragOutside method.
mahboudz
You should play with these yourself to see how it behaves. In Xcode make a bunch of IBAction methods that just call NSLog with different messages. In InterfaceBuilder, create a single button and wire the different events to the different methods. Then run the app, tap, drag, etc. until you see how each event gets triggered.
mahboudz
Thanks again. I tried ur suggestions but i think, directly using button's events will not help me. I m able to know user has started touches in same button and ended in other button but when user moves start dragging from one button and continues it for next button i want events for the button where user is? like piano keys.
Niraj
A: 

You need to handle the touches yourself from a UIWindows subclass, or from the UIViewController container (in this case remember to disable the userInteractionEnabled property)

Dzamir