【发布时间】:2016-03-25 17:05:25
【问题描述】:
我有一个垂直的 UIScrollView,它有几个大按钮占据了滚动视图中的大部分空间。出于这个原因,我不得不继承这篇文章ios 8 - buttons in horizontal scroll view intercepting pan event - scroll does not work) 之后的滚动视图并覆盖以下类方法:
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
static int count = 0;
count++;
NSLog(@"calling this function 2");
NSLog(@"count = %d", count);
UITouch *touch = [touches anyObject];
if(touch.phase == UITouchPhaseMoved)
{
NSLog(@"UITouchPhaseMoved so returning no");
return NO;
}
else
{
NSLog(@"returning super");
NSLog(@"returning whatever this value is %d 0/1", [super touchesShouldBegin:touches withEvent:event inContentView:view]);
[super touchesShouldBegin:touches withEvent:event inContentView:view];
return [super touchesShouldBegin:touches withEvent:event inContentView:view];
}
}
当我运行我的应用程序时,点击按钮时会调用上述函数,但当我在滚动视图中拖动按钮内部或外部的任何位置时不会调用该函数。而且无论我尝试拖动到哪里,滚动视图都不会滚动。
这是我所有与滚动视图相关的代码(它是用 IB 设置的):
smallView.scrollEnabled = YES;
smallView.contentSize = CGSizeMake(smallView.frame.size.width, smallView.frame.size.height*1.4);
smallView.delegate = self;
这里是按钮相关的代码,以防万一:
NSArray *buttonsArray = @[button1, button2, button3, button4, button5];
for (int i = 0; i < [buttonsArray count]; i++) {
UIButton *button = buttonsArray[i];
button.tag = i+1;
[button addTarget:self action:@selector(p_motivationButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
}
为什么我的滚动视图没有滚动,甚至没有调用touchesShouldBegin?
【问题讨论】:
-
你在 touchphase 移动时返回 no 吗?
-
@TejaNandamuri 我不知道,这可以改变,但函数永远不会进入逻辑的那个分支。
-
你在哪里实现了触摸移动方法? touches begin 方法无法拦截touched 移动事件。它只会检测触摸事件的起点。
-
它解释了为什么滚动事件没有发送到子视图。
标签: ios objective-c iphone uiscrollview