views:

2458

answers:

1

I have several UIButtons which I use to set the current action when tapping in the main area. I would also like to allow the user to drag from the button directly into the main area and take the same action; essentially, the touchesBegan and touchesMoved should be passed on to the main view when touching the UIButtons, but also should send the button press action.

Right now, I have the touch up inside changing the control. The drag exit calls the touch up inside section to set the control, then calls the touches began section to start the main area touching operation.

However, at this point, the touchesMoved and touchesEnded are obviously not being called, because the touches originated on the UIButton.

Is there a way to half-ignore the touches so they're passed to the main area, but also allow me to set the control first?

+8  A: 

In the documentation, look for Responder Objects and the Responder Chain

You can "share" touches between objects by forwarding the touch up the responder chain. Your UIButton has a responder/controller that receives the UITouch events, my guess is that once it has preformed its interpretation of the message it returns - the touch has been handled and disposed of.

Apple suggests something like this (based on the type of touch of course):

[self.nextResponder touchesBegan:touches withEvent:event];

Rather than disposing of the touch event it is passed on.

Sub classed UIButton:

MyButton.h

#import <Foundation/Foundation.h>

@interface MyButton : UIButton {

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event ;

@end

MyButton.m

#import "MyButton.h"

@implementation MyButton

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
    printf("MyButton touch Began\n");
    [self.nextResponder touchesBegan:touches withEvent:event]; 
}
@end
Paxic
So in this case, I would have to subclass UIButton to pass the touches on, I presume?
Ed Marty
Subclassing should not be necessary, you will need to explore the chain a bit. The view or control that contains your button will be the next responder in the chain.
Paxic
I'm afraid I don't quite see how to trap messages from the UIButton and pass them on to the UIView without subclassing the UIButton to actually get the message.
Ed Marty
You are right, I was not thinking clearly. The easiest way to do this is by sub classing. You might want to look at editing the responder chain but sub classing will give you the result now. Sorry for goose chase.
Paxic
It's not necessary to declare the touchesBegan:: method in the header, as it is just being overridden, right?
jbrennan
You had a link above to the docs. I've been having this same problem and just read in those docs: "If you implement a custom view to handle events or action messages, you should not forward the event or message to nextResponder directly to send it up the responder chain. Instead invoke the superclass implementation of the current event-handling method—let UIKit handle the traversal of the responder chain."
Spanky