简体   繁体   中英

how can i detect a touch with a scroll view?

I want some methods to be run when my user touches the screen, so implemented the touchesBegan:withEvent: method in my ViewController, but when I drag and dropped the scroll view onto my iphone, the touchesBegan:withEvent: method no longer gets called.

To try and fix I implemented the becomeFirstResponder method in my ViewController and told it to return YES; and then in viewDidAppear method i called [self becomeFirstResponder];

however, after all this it did not work.

I also tried creating a custom scroll view and creating a ViewController object property in it, and then in the touchesBegan:withEvent: method for my custom scroll view I implemented it saying [self.mainViewController touchesBegan:withEvent] so hopefully every time i touched the screen the touchesBegan method on my custom scroll view would get called, and then because of that my touchesBegan method in my vc would get called, but that wouldn't help either.

I also made an outlet of my scroll view to my vc and added a tap gesture recognizer to it and initiated it with a method and stuff and that worked the method i initiated with it got called but it was only after i touched up inside the screen, i want the method to get called as soon as i touch down the screen

any answers are muchly appreciated!

  1. Create a subclass of UIScrollView
  2. override Touches end and if the user clicked instead of dragged I manually forward the message to first responder.

      @interface MyScrollView : UIScrollView { } @end @implementation MyScrollView - (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event { if (!self.dragging) { [self.nextResponder touchesEnded: touches withEvent:event]; } [super touchesEnded: touches withEvent: event]; } @end 

In order to do this I just made my own custom scroll view class, and implemented the touchesBegan:withEvent: method in the uiscroll view with a delegate method I made.

So basically i just had to use delegation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM