我在我的简单故事板IPad项目上有UIViewController,它包含放置在整个表面(1024 x 768)上的UIScrollView.我创建了3个XIB文件,这些文件是我的应用程序在viewDidLoad中启动时加载的UIViews,并将它们添加到UIScrollView中.这3个XIB文件中的每一个只包含一个UIButton.
这是层次结构:
~UIViewController(UIViewControllerClass是这个UIViewController的类)
~~ UIScrollView(包含3个相同的UIViews)
~~~ UIView(UIViewClass是此XIB文件的文件所有者)
~~~~ UIButton
我希望我的UIViewControllerClass能够识别两者:触摸UIScrollView组件上的任何位置,如果触摸了UIScrollView,如果触摸了UIScrollView中UIView内的UIButton,则可以获得完全触摸该按钮的信息.
我在UIViewClass中创建了IBAction,用于触摸UIScrollView中UIView内的UIButton,当我在所有元素(UIViewController,UIView和UIScrollView)上设置User Interaction Enabled = YES时,会调用此方法.
但此时我的UIViewControllerClass并不知道在UIButton上的UIScrollView内发生了触摸.我做了这样的触摸识别器:
UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouch)];
touch.numberOfTouchesRequired = 1;
Run Code Online (Sandbox Code Playgroud)
并将其添加到UIScrollView组件.通过这种方式,我能够在UIViewControllerClass中检测UIScrollView组件上的触摸事件,但是UIView中的UIButton触摸事件处理程序不再被调用.
所以,我需要在UIViewControllerClass中有这两个信息:
我认为将触摸事件识别器附加到整个UIScrollView组件不是解决方案,因为它禁用了我在UIViewClass中编写的所有触摸事件处理程序.
我认为解决方案是,在UIScrollView中的UIView组件上进行的某些操作应该发送到UIViewControllerClass,但我没有找到一种方法来执行此操作.
如果有人能帮助我,我会非常感激.提前致谢.
[编辑#1:郑的回答]
点击手势必须将cancelsTouchesInView选项设置为NO!
对于我的上述情况,这条线解决了一切:
touch.cancelsTouchesInView = NO;
Run Code Online (Sandbox Code Playgroud)
非常感谢郑.
似乎按钮根本没有被按下.
我试过了:
然而,这是捕获
如果UIPageViewController的页面转换是UIPageViewControllerTransitionStyleScroll那么
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
NSLog(@"%@",self.pageViewController.gestureRecognizers.description);
NSLog(@"%@",self.pageViewController.gestureRecognizers.description);
Run Code Online (Sandbox Code Playgroud)
只会是空的.
我尝试将自己的gestureRecoqnizers而不是按钮添加到UIViewControllers的子视图中.它仍然无法正常工作.
所以我有一个UIPageViewController,它显示一个UIViewController,它的视图有一些按钮.如何按下该按钮?
我也在这里试过两个解决方案:
http://emlyn.net/posts/stopping-gesture-recognizers-in-their-tracks
没有用.首先,我无法禁用UIPageViewController的手势重新整理器,因为没有这样的东西.在scrollView模式下,UIPageViewController没有手势重新整理器.
我试图将自己的手势重新整理器添加到我自己的按钮,但这些从未被调用过.
我希望UIPageViewController仍然可以处理滑动.但不要点击.
我无法访问UIPageViewControllers的手势重新生成器,因为self.pageViewController.gestureRecognizers为空.UIPageViewController似乎只是"吸收"所有点击事件.所以我的按钮没有得到它.
我可以在UIPageViewController前添加按钮,但该按钮将吸收我希望UIPageVIewController仍然处理的滑动动作.
顺便说一句,如果我们使用来自IOS的模板(创建基于页面的应用程序,并更改过渡以滚动pageviewcontroller.gesturerecoqnizers将为空
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Configure the page view controller and add it as a child view controller.
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];//Turn this to scroll view
self.pageViewController.delegate = self;
SDDataViewController *startingViewController = [self.modelController viewControllerAtIndex:0 storyboard:self.storyboard];
NSArray *viewControllers = @[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward …Run Code Online (Sandbox Code Playgroud) 我在情节提要中的 UIScrollView 上设置了 UITapGestureRecognizer。Scroll View 包含其他内容(两个 UIView,一个 UIWebView)。
手势识别器属性如下:
Scroll View(相关)属性如下:
但是,当我点击滚动视图上的任意位置时,手势不起作用。
我将所有 UIElement 移动到滚动视图中以避免加载 UIViewcontroller。但现在视图上的 GestureRecognizers 不再起作用了。
我的视图层次结构如下所示:
- UIViewController(单事件控制器)
- UIScrollView (EventScrollView)
- UIView(内容视图)
- UILabel、UIView、UITableView 等
这看起来是一个常见问题,因为我在 Stackoverflow 上发现了很多类似的问题: 示例 1 示例 2。尽管如此,我还是没能解决我的案子。
我的EventScrollView中的简化代码如下所示:
let locationLabel: UILabel = {
let label = UILabel()
label.text = "Standort"
label.isUserInteractionEnabled = true //Important!
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.isUserInteractionEnabled = true //not needed!
contentView.isUserInteractionEnabled = true //Important!
}
func setupViews() {
guard let parent = parentVC else { return }
let locationTap = UITapGestureRecognizer(target: parent, action: #selector(parent.openInGoogleMaps))
locationTap.cancelsTouchesInView = false …Run Code Online (Sandbox Code Playgroud)