Delta Engine Blog

All about multiplatform and game development

Passing touch events from UIScrollView to the parent UIViewController

This post is about Objective-C iPhone stuff!

Just because this took me some time to figure out (and might be useful in the future or for other people): In the iPhone SDK a UIScrollView class will eat up all touch events (touchesBegan, touchesEnded, touchesMove, etc.) and not pass them along to your view controller where all your view logic might be (like in my case). If you know this, you can just create a new class for each ScrollView and then have some of your logic there, but in my case all I want is to pass these events along to the UIViewController (like all the other controls behave like). For this reason I just created a simple class called PassTouchesScrollView, which looks like this:
//  PassTouchesScrollView.m
//  Simple helper class for UIScrollViews that need to pass touchesBegan to the
//  UIViewController above it (see all the Controller classes here).
#import "PassTouchesScrollView.h"

@implementation PassTouchesScrollView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
	UIView *result = nil;
	for (UIView *child in self.subviews)
		if ([child pointInside:point withEvent:event])
			if ((result = [child hitTest:point withEvent:event]) != nil)
				break;

	return result;
}

- (void)dealloc
{
    [super dealloc];
}

@end

Note: This is only useful if you really do not need any touch events in the UIScrollView because even the scrolling drag touch events will be ignored and send to the UIViewController. If you want to be more selective, e.g. just pass the touchesEnded event on to the parents via:
-(void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
	// Pass to parent
	[super touchesEnded:touches withEvent:event];
	[self.nextResponder touchesEnded:touches withEvent:event];
}

References: Found helpful tips on StackOverflow and other sites (including this apple support forum).

Comments (15) -

  • call tracking

    4/10/2010 7:32:36 AM |

    iPhone is the big widescreen iPod whose screen resolution is 320x480 which is bigger than the video iPod's 320x240. Many mobile phones supports 3GP, 3G2 formats, but iPhone does NOT. It support H.264, MPEG-4 in .mp4, .m4v formats, and MP3, protected AAC, AAC, Audible, Apple lossless in .aac, .mp3 files.

  • Naples Realtor

    4/13/2010 6:45:48 PM |

    Interesting post you got here. I think I will have to read more later.  Thanks for the info.  Bookmarked.

  • internet providers

    4/15/2010 8:54:35 AM |

    It is really impressive to share these useful contents and I like the best creation made by the PassTouchesScrollView and thank the members.

  • hyip

    4/23/2010 9:48:07 AM |

    I become a i-phone user since a week ago. I really love its applications and the features. Thanks for the additional info.

  • atlantic optimize

    4/23/2010 2:30:51 PM |

    My applications really works good and these help much better to know more stuff involved in the i-phones. I had made a big collection of it. And sharing with all my friends.

  • scalp pimples

    4/26/2010 3:18:37 AM |

    I like the best creation made by the PassTouchesScrollView.I really love its applications and the features.Many mobile phones supports 3GP, 3G2 formats, but iPhone does NOT. It support H.264, MPEG-4 in .mp4, .m4v formats, and MP3, protected AAC, AAC, Audible, Apple lossless in .aac, .mp3 files.

  • course defensive driving

    4/26/2010 4:44:58 AM |

    Admiring the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and have my children check up here often. Thumbs up!

  • 25th birthday party nyc

    4/26/2010 8:43:19 AM |

    I like it very much especially the information you have putted here is like training. Keep the blog up to date.I like the best creation made by the PassTouchesScrollView.It support H.264, MPEG-4 in .mp4, .m4v formats, and MP3, protected AAC, AAC, Audible, Apple lossless in .aac, .mp3 files.

  • Boulder Property

    4/28/2010 3:10:00 PM |

    This is a quality share on the i-phone stuff. I want is to pass these events along to the UIViewController (like all the other controls behave like). For this reason I just created a simple class called PassTouchesScrollView

  • Home Remodeling

    5/3/2010 8:46:17 AM |

    Here made some good notifications. This is only useful if you really do not need any touch events in the UIScrollView because even the scrolling drag touch events will be ignored and send to the UIViewController.

  • gay dating

    5/8/2010 1:10:37 PM |

    Thanks a lot for enjoying this beauty article with me. I am apreciating it very much! Looking forward to another great article. Good luck to the author! all the best!

  • debt solutions

    5/17/2010 7:38:59 AM |

    I really look forward to what you post frequently. I have bookmarked this so that I can return whenever I want uptodate info that I can pick and use right away. I love the way you are able to put into words what you feel and how it is able to work instantaneously. Keep up the posting and flow of thoughts.

  • bathroom taps

    5/18/2010 2:29:26 PM |

    This made a great difference between the UIview controller to the parent UIview controller. I have found the useful information which was really important for me in your site. Reading this your post I have known many new things about this great new blog which I have not known before. It is really great that you have created for us such useful stuff.

Comments are closed