Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to setup a subclass of UIScrollView to respond to touch events?
    primarykey
    data
    text
    <p>I recently had a problem regarding not being able to register touches events (<code>touchesMoved</code>, etc) from within a <code>UIScrollView</code>. After talking with lots of people and reading tons of posts, it turns out <code>UIScrollView</code> does't accept touches itself, and instead the touches need to be passed to a <code>UIScrollView</code> subclass.</p> <p>I am very new to objective-c, and am having a terrible time wrapping my head around how subclasses are defined (in separate .h/.m files or somehow in @interface or something), as well as how iOS deals with responder chains. </p> <p>Please let me describe my exact situation, and anyone willing to explain what I need to do in noob-speak will be really appreciated. </p> <p>I have a single page in my app, and I changed it's default <code>UIView</code> to <code>UIScrollView</code>, so if I inspect it in <code>InterfaceBuidler</code> all I see is my <code>SampleViewController</code> and <code>UIScrollView</code>. I linked my <code>UIScrollView</code> to the <code>@Interface</code> of <code>SampleViewControler.h</code> like this:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface SampleViewController : UIViewController { } @property (strong, nonatomic) IBOutlet UIScrollView *mainScroller; @end </code></pre> <p>And I referenced my <code>UIScrollView</code> in <code>SampleViewController.m</code> like this:</p> <pre><code>#import "SampleViewController.h" @interface SampleViewController () @end @implementation SampleViewController @synthesize mainScroller = _mainScroller; - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Movement!"); } -(void)viewWillAppear:(BOOL)animated { [_mainScroller setContentOffset:CGPointMake(0,0) animated:YES]; } </code></pre> <p>If I change the class of <code>UIScrollView</code> to <code>UIView</code>, the touches are detected and I fire off a few <code>NSLog</code> messages. However, once <code>UIView</code> is set back to <code>UIScrollView</code>, touches are ignored.</p> <p>From what I understand I need to setup a subclass of <code>UIScrollView</code> with the code:</p> <pre><code>- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Movement!"); } </code></pre> <p>...and then change my SampleViewController.m to have the code:</p> <pre><code>- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [[self.nextResponder nextResponder] touchesMoved:touches withEvent:event]; } </code></pre> <p>However, I really don't understand 1) how to setup such a subclass, and 2) why this would work, since I don't see why <code>[[self.nextResponder nextResponder] touchesMoved:touches withEvent:event];</code> would ever be called in the first place from within <code>SampleViewController.m</code>, since my similar <code>NSLog(@"Movement!");</code> statement is never executed.</p> <p>I have tried every tutorial I could find, and am turning toward to the expertise of SE! Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload