Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Detect touch in scrollview - tutorial

2»

Replies

  • dennisollydennisolly Posts: 1New Users
    edited June 2010
    slahteine wrote: »
    What result do you get if you return NO from touchesShouldCancelInContentView?

    Also, you shouldn't be handling any touches in the ScrollView itself, only in the content views within it.

    @slahteine

    Man, you rule! They should call you a Programming Wizard. oh wait...

    Seriously, thanks for those tips! I now have UIScrollView that scrolls like it should and can be swiped away (or whatever response you like) with a single swipe.

    Dennis
  • kumkum Posts: 1New Users
    edited July 2010
    Great tutorial. However I still have some issues.

    I enabled paging so that I could scroll through different views. The problem is that only the first view is able to detect touches and the others can't. Why is this?
  • michael.liebweinmichael.liebwein Posts: 1New Users
    edited September 2010
    kum wrote: »
    Great tutorial. However I still have some issues.

    I enabled paging so that I could scroll through different views. The problem is that only the first view is able to detect touches and the others can't. Why is this?

    This might be the case, when you have not set the size of the scroll view's content view correctly. So if you have a scrollView and a contentView as it's child (containing your different views), you should be doing something along the lines:
    CGRect contentFrame = contentView.frame;
    contentFrame.size.width = newWidth; // set new width here
    contentFrame.size.height = newHeight; // set new height here
    contentView.frame = contentFrame;
    
    scrollView.contentSize = contentView.frame.size;
    
  • cvcs1cvcs1 Posts: 43Registered Users @
    edited March 2011
    Great Tutorial! Really helpful.

    I have one question though, How do I check WHERE it is touched? I have this code in my "myImageView"
    -(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
    {	
    	UITouch *touch = [touches anyObject];
    	CGPoint touchPoint = [touch locationInView:self];
    	NSLog(@"Touch detected at: X = %d, Y = %d", touchPoint.x, touchPoint.y);
    }
    

    But that returns this in the log:
    2011-03-11 07:05:56.969 Us![3428:207] Touch detected at: X = 0, Y = 1085492480
    2011-03-11 07:05:57.322 Us![3428:207] Touch detected at: X = 0, Y = 1085492480
    2011-03-11 07:05:57.529 Us![3428:207] Touch detected at: X = 0, Y = 1085492480
    

    And that is DEFINITELY not where I touched it, and I moved quite a lot to try and get something else. Thanks!

    EDIT: Nevermind... I was logging it incorrectly. :P
  • Smith01Smith01 Posts: 1New Users
    edited March 2011
    Cool posts indeed.
    Please carry on guys with more cool tutorials.
  • Jasika8745Jasika8745 Posts: 1New Users
    edited March 2011
    A very good work done by you.
  • MontxoMontxo Posts: 6New Users
    edited June 2012
    Hello, I am a newbie.
    Everybody talk about solutions to detect touches over a Scrollview using delegation, subclassing, etc. I am lost yet on those concepts.
    I need to detect a single touch over scrollviews. I cannot find a source code project to see how this is implemented.
    I have prepared a simple project to explain my problems and to try to find somebody to write the necesary code or subclass or whatever to help me to understand "in real life" how this is implemented.

    Please find attached my project. Any help is appreated.
    BTW: I am reading books about delegation, classes, subclassed, objective-C, etc. but not yet understanding.
    Sorry for my english.

    Thanks in advance.

    Montxo Garcia
    Spain
  • MontxoMontxo Posts: 6New Users
    edited June 2012
    Hello,
    can anybody to attached a complete "compiling" project for this?
    I am newbie so there are many comments to make things that I dont know how to do. Still learning about concepts, etc,

    THanks in advance.
    Montxo Garcia
  • hakimnyhakimny Posts: 72Registered Users
    edited June 2012
    Hello Montxo

    All you need to do is Create a class lets say we call it MyScrollView

    make it first a subclass of NSObject ( not necessary just making it easy for you). when using the wizard

    so You will end up with MyScrollView.h looking like

    @interface MyScrollViewiPhone : NSObject

    change it to :
    @interface MyScrollViewiPhone : UIScrollView

    here you just subclassed UISCrollView

    All you need to do is now tell you subclass how to handle Touches, so you need to override the method.( I am assuming you need only to detect a touch usually touchesEnded method is enough)
    -(void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event 
    {
           if (!self.dragging) 
          {
            // This part should written if you want to isolate only touches
            [self.nextResponder touchesEnded: touches withEvent:event];
    
          // loop thru all touches
          for(UITouch *touch in touches)
         {
    
           // if you need to detect a partcular touch that is linked to a particular subview of your scrollview
          // one trick you need to add is tagging your subview so you can reference them by tag# (.i.e UILabel *myLabel =[[UILabel alloc] ......,  myLabel.tag = 1 )
    
          //loop thru all your subviews
         for(int i=1 to i<=[[self.subviews] count]; i++)
        {
           // check if position where finger touched the screen coincide with one of your subviews
    
              if(CGRectContainsPoint([[self viewWithTag:i]frame], [touch locationInView:self]))
                    {             
    				//Your logi here
                    }
       }
    
          }
    
          }
          else
          {
             //this part should be written if you need to work on functionality when ScrollView is dragging.
    
    
    
    
         }
    
          // Very Important this tell this method to use parent method so it knows all the functionality ( without entering into deep details)
    	[super touchesEnded: touches withEvent: event];
    
    }
    
    

    FYI I have not written this code (just modified it to my needs), I used it like 3 years ago, I apologize i can't seem to find the origin.

    Hakim
  • MontxoMontxo Posts: 6New Users
    edited June 2012
    Hi Hakim,
    Thanks for your response but... I've got two warnings "Use of undeclared identifier 'to'" and "Expect ';' in 'for' statement specifier" in the line:
    for(int i=1 to i<=[[self.subviews] count]; i++)

    kind regards,
    Montxo
  • hakimnyhakimny Posts: 72Registered Users
    edited June 2012
    Montxo wrote: »
    Hi Hakim,
    Thanks for your response but... I've got two warnings "Use of undeclared identifier 'to'" and "Expect ';' in 'for' statement specifier" in the line:
    for(int i=1 to i<=[[self.subviews] count]; i++)

    kind regards,
    Montxo

    There is a typo in my statement it should be:
    for(int i=1 ; i&lt;=[[self.subviews] count]; i++)
    
  • MontxoMontxo Posts: 6New Users
    edited June 2012
    Oppps,
    I am too newbie to detect this typo. Sorry. I am reading a lot the basics yet.
    Thanks a lot.
    Montxo
2»
Sign In or Register to comment.