A problem with override UINavigationController - iphone

now i'm working with cocos2d and i design to add navigationcontroller to my cocos2d application,
so i add navigationcontroller to my application when i click it not pass touch or event to cocos2d
now i'm try to override UINavigationController by
add new new class name is NavigationController and inherit from UINavigationController
in init i call [super init];
every things look be ok
but when i try to add
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Im overriding touch");
return YES;
}
- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Im overriding touchMove");
return YES;
}
it not call

Why are you calling the methods ccTouchesBegan:withEvent: and ccTouchesMoved:withEvent: instead of the original names? You don't have to change the names of the methods when you subclass UINavigationController; instead, you should keep the same names and call super on them as well as appropriate. For example:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"I'm overriding touch");
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"I'm overriding move");
[super touchesMoved:touches withEvent:event];
}

Related

touchesMoved detection inside a subview

I am trying to detect touches inside a subview
In my main viewcontroller I am adding a subview called:SideBarForCategory, it takes 30% of the screen from the left - as a sidebar.
SideBarForCategory *sideBarForCategory = [[SideBarForCategory alloc] initWithNibName:#"SideBarForCategory" bundle:nil];
[sideBarData addSubview:sideBarForCategory.view];
inside SideBarForCategory, I would like to test for touches
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint location = [touch locationInView:touch.view];
NSLog(#"Map Touch %f",location.x);
}
The above code (touchesMoved) works perfectly on the main view (viewcontroller) but does not work inside my subview (SideBarForCategory) - why and how can I fix it?
Two possible solutions i can think of :
Either use GestureRecognizers (eg. UITapGestureRecognizer, UISwipeGestureRecognizer) and add those recognizers to your SideBarForCategory view.
Generic gesture handling : Create your own custom subclass of UIView for example MyView and add those touch methods inside it. Then create SideBarForCategory view as an instance of MyView.
Hope it works :)
Updated :
For second option :
#import <UIKit/UIKit.h>
#interface MyView : UIView
#end
#implementation MyView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// No need to invoke |touchesBegan| on super
NSLog(#"touchesBegan");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// invoke |touchesMoved| on super so that scrolling can be handled
[super touchesMoved:touches withEvent:event];
NSLog(#"touchesMoved");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
NSLog(#"touchesEnded");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
/* no state to clean up, so null implementation */
NSLog(#"touchesCancelled");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
Update :
And then in your SideBarCategoryView class implementation , inside loadView()
self.view = [[MyView alloc] init];
check Apple's references for converting points. You probably forget to convert the point relevant to your subview. Therefore it is trying to check the absolute coordinates of the touch point.
Probably what you need is :
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view

Custom UITableViewCell with UIScrollView

Cell doesn't receive touch events, when there is UIScrollView inside UITableViewCell. Is there any way to cancel tap events for UIScrollView (needs only to handle scrolling)?
If you need touches to go through, implement a subclass of UIScrollView, and add these:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Pass to parent
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// Pass to parent
[super touchesEnded:touches withEvent:event];
[self.nextResponder touchesEnded:touches withEvent:event];
}
The cell only interecepts taps, so it'll work.
This is brilliant! I was pulling my hair on this one.

detecting the [UITouch view] in touchesMoved method

I wish to drag from one subview to another within my application (and connect them with a line), and so I need to keep track of the current view being touched by the user.
I thought that I could achieve this by simply calling [UITouch view] in the touchesMoved method, but after a quick look at the docs I've found out that [UITouch view] only returns the view in which the initial touch occurred.
Does anyone know how I can detect the view being touched while the drag is in progress?
And my way:
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([self pointInside:[touch locationInView:self] withEvent:event]) {
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
} else {
[self sendActionsForControlEvents:UIControlEventTouchUpOutside];
}
}
After a bit more research I found the answer.
Originally, I was checking for the view like so:
if([[touch view] isKindOfClass:[MyView* class]])
{
//hurray.
}
But, as explained in my question, the [touch view] returns the view in which the original touch occurred. This can be solved by replacing the code above with the following:
if([[self hitTest:[touch locationInView:self] withEvent:event] isKindOfClass:[MyView class]])
{
//hurrraaay! :)
}
Hope this helps
UIView is subclass of UIResponder. So you can override touches ended/ began methods in your custom class, that inherits from UIView. Than you should add delegate to that class and define protocol for it. In the methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
or whatever interactions you need just send appropriate message to object's delegate also passing this object. E.g.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[delegate touchesBeganInView: self withEvent: event];
}

Parent UIScrollView detecting child UIView's touches

Basically...
I have a UIScrollView subclass which you can add a target and selector too, which get fired if a touch event is detected within the view. I am using the scroll view as an image gallery, and the touch inside the scroll view is used to fade out the HUD components (UItoolBar, etc):
#implementation TouchableScrollView
- (void)addTarget:(id)_target withAction:(SEL)_action
{
target = _target;
action = _action;
shouldRun = NO;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
shouldRun = YES;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
shouldRun = NO;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(shouldRun)
{
shouldRun = NO;
[target performSelector:action];
}
}
#end
I then have another custom UIView added as a subview to this which has the following set (both of which I have played around with):
self.userInteractionEnabled = YES;
self.exclusiveTouch = YES;
and uses the following to trigger an animation:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
The problem is even when the custom UIView detects the touch and responds (by flipping itself over), the UIScrollView selector also gets fired causing everything to fade out.
Please help, I'm totally stuck?!
better to use this in custom ScrollView class
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
if (!self.dragging) {
[self.nextResponder touchesEnded: touches withEvent:event];
}
[super touchesEnded: touches withEvent: event];
self.nextResponder is scrollView subViews. so event other then dragging (scrolling) will be handled by your main viewController . in main View controller inherit touch event method. there u can check for touch event for particular view or image view.

Is there a way to pass touches through on the iPhone?

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?
I know this question is two years old (and already answered), but nevertheless...
When I tried this myself, the touches were forwarded, but the buttons no longer behaved like buttons. I also passed the touches along to "super" and now all is well.
So, for beginners that might stumble upon this, this is what the code should look like:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
}
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
No need to subclass either! Simply stick this on the top of your implementation before anything else:
#pragma mark PassTouch
#interface UIButton (PassTouch)
#end
#implementation UIButton (PassTouch)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
[self.nextResponder touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
[self.nextResponder touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
[self.nextResponder touchesCancelled:touches withEvent:event];
}
#end