Transcript of "How to invoke touch methods on UI Scroll View in iPhone app"
How do you invoke TouchMethods on UIScrollView in iPhone Application? 11/1/2011
Introduction First fundamental I want to clear here is that when a touch occurs on UIScrollView in iPhone app, how do you differentiate if it was meant for scrolling or it was meant for tracking a subview? I will give you an example, suppose you have ScrollView of width say 640px and you have added 4 buttons on ScrollView as its subview. When you touch on ScrollView how does system know that the touch was meant to press buttons or it was meant to scroll the ScrollView. Let me explain the inside story, when user makes a touch on ScrollView in iPhone, a timer is started , if the timer fires without a significant change in position then ScrollView send signal to methods of touched subview in the content view. If user drags their finger far enough before timer dies then scrolling happens in ScrollView. 11/1/2011
…Continued Now lets come to our main point. You have embedded UIScrollView on your screen, how do you know which coordinate user touched on iPhone screen? TouchBegan, TouchMove, TouchEnded don’t work on UIScrollView. The delegate methods available for UIScrollView don’t cover this explicitly but there is a way around it. I am going to cover this. We need to create a subclass of UIScrollView. 11/1/2011
Code SnippetI am covering here only the basic implementation, you can customize it todo much more stuff according to your needs. 11/1/2011
Next step is I have created an instance of this subclass of UIScrollView in oneof my ViewController class. I can call normal UIScrollView delegate methodson it as I have done in the code above 11/1/2011
…Continued Now when I tap on screen with ScrollView on it, it will identify where it was a tap or it was meant to be scroll the ScrollView. If it was a tap it will access - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view in MyScrollView.m and print the coordinate of the touch location. There you go ! This is how you invoke Touch Methods on ScrollViews. 11/1/2011