简体   繁体   English

如何使用滚动视图检测触摸?

[英]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. 我希望在用户触摸屏幕时运行某些方法,因此在ViewController中实现了touchesBegan:withEvent:方法,但是当我将滚动视图拖放到iPhone上时,touchesBegan:withEvent:方法不再被调用。

To try and fix I implemented the becomeFirstResponder method in my ViewController and told it to return YES; 为了解决这个问题,我在ViewController中实现了beginFirstResponder方法,并告诉它return YES; and then in viewDidAppear method i called [self becomeFirstResponder]; 然后在viewDidAppear方法中,我调用了[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. 我还尝试创建一个自定义滚动视图并在其中创建一个ViewController对象属性,然后在我的自定义滚动视图的touchesBegan:withEvent:方法中实现了它说[self.mainViewController touchesBegan:withEvent]所以希望每次我触摸屏幕将调用我的自定义滚动视图上的touchesBegan方法,然后由于该原因,我的vc中的touchesBegan方法将被调用,但这也无济于事。

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 我还向我的vc制作了一个滚动视图的出口,并向其中添加了一个轻击手势识别器,并使用一种方法和一些东西来启动它,并且该方法以我启动的方法被调用了,但这只是在我触摸了屏幕内部之后,我希望在触摸屏幕后立即调用该方法

any answers are muchly appreciated! 非常感谢任何答案!

  1. Create a subclass of UIScrollView 创建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. 为此,我只创建了自己的自定义滚动视图类,并使用我创建的委托方法在uiscroll视图中实现了touchesBegan:withEvent:方法。

So basically i just had to use delegation. 所以基本上我只需要使用委托。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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