我UIScrollView添加了一个单击手势识别器来显示/隐藏一些UI叠加使用:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[scrollView addGestureRecognizer:singleTap];
Run Code Online (Sandbox Code Playgroud)
和:
- (void)handleTap:(UITapGestureRecognizer *)sender {
// report click to UI changer
}
Run Code Online (Sandbox Code Playgroud)
我在底部添加了一个简单的表格视图UIScrollView.一切正常(水平和垂直滚动)但问题是点击仅由手势识别器(上图)识别,而不是由简单的表格视图识别.如果我删除注册手势监听器的行,一切正常,表视图会通知自己.
这就好像手势识别器功能"吃掉"表视图上的点击事件并且不向下传播它们.
任何帮助表示赞赏
我在UITableView内心问题UIScrollView.当我滚动外部时scrollView,第一次触摸时table不会收到willSelect/didSelect事件,但第二次触摸时不会收到事件.更奇怪的是,细胞本身会得到触摸和突出显示的状态,即使delegate没有.
我的视图层次结构
UIView
- UIScrollView (outerscroll)
- Some other views and buttons
- UITableView (tableView)
Run Code Online (Sandbox Code Playgroud)
在滚动视图中,我有一些额外的视图可以动态扩展/关闭.表格视图需要与视图中的其他一些元素一起"固定"在顶部,这就是我创建此布局的原因,这使我可以通过使用转换以类似于Apple推荐的方式轻松移动元素.滚动发生了.
当outerscroll像这样移动时,表视图将使用转换效果进行转换:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.outerScrollView) {
CGFloat tableOffset = scrollView.contentOffset.y - self.fixedHeaderFrame.origin.y;
if (tableOffset > 0) {
self.tableView.contentOffset = CGPointMake(0, tableOffset);
self.tableView.transform = CGAffineTransformMakeTranslation(0, tableOffset);
}
else {
self.tableView.contentOffset = CGPointMake(0, 0);
self.tableView.transform = CGAffineTransformIdentity;
}
// Other similar transformations are done here, but not involving …Run Code Online (Sandbox Code Playgroud) 我在我的简单故事板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)
非常感谢郑.
我的UIScrollView内容视图中的UILabels上的UITapGestureRecognizer没有调用它的方法.
视图层次结构如下:
我已经将代码简化为一个示例以突出显示该问题
// Set scrollview size - Added in Storyboad
[scrollView setContentSize:CGSizeMake([arrayOfVerbs count]*self.view.frame.size.width, scrollView.contentSize.height)];
[scrollView setCanCancelContentTouches:YES]; // Tried both yes and no
[scrollView setPagingEnabled:YES];
// Add content view
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)];
[scrollView addSubview:contentView];
// Add test UILabel
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
[testLabel setBackgroundColor:[UIColor redColor]];
[testLabel setText:@"Test touch"];
[testLabel setUserInteractionEnabled:YES];
[contentView addSubview:testLabel];
// Add gesture recogniser
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playSound:)];
singleTap.numberOfTapsRequired …Run Code Online (Sandbox Code Playgroud)