How to invoke touch methods on UI Scroll View in iPhone app


Published on

How do you invoke Touch Methods on UIScrollView in iPhone Application?

Published in: Technology, News & Politics
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

How to invoke touch methods on UI Scroll View in iPhone app

  1. 1. How do you invoke TouchMethods on UIScrollView in iPhone Application? 11/1/2011
  2. 2. 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
  3. 3. …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
  4. 4. Code Snippet 11/1/2011
  5. 5. Code SnippetI am covering here only the basic implementation, you can customize it todo much more stuff according to your needs. 11/1/2011
  6. 6. 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
  7. 7. …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
  8. 8. 11/1/2011