UIScrollView无法响应触摸

3

我有一个UIScrollView,用于显示推文。这个scrollView包含在一组视图中,最后一个视图有4个手势识别器。

scrollView的填充方式如下:

- (void) createTimeLine:(NSArray*)timeline
{
    CGRect rect = CGRectMake(0, 0, 463, 150);
    NSInteger i = 0;
    NSLog(@"timeline objects: %d", timeline.count);
    self.scrollView.contentSize = CGSizeMake(463, timeline.count * 150);
    for (NSDictionary *d in timeline) {
        SingleTwitViewController *c = [[SingleTwitViewController alloc] initWithAsingleTwit:d];
        rect.origin.y = i * 150;
        c.view.frame = rect;
        [scrollView addSubview:c.view];
        [scrollView bringSubviewToFront:c.view];
        i += 1;
    }
}

时间轴中有20个对象,前4个是可见的。然而,scrollView不滚动并且触摸由手势识别器处理。

scrollEnabled属性被设置为YES,但出于某种原因,它似乎不起作用。

contentSize被设置为463 * 3000,远小于scrollView本身,但仍然无法滚动。

有什么想法?


你是否为你的scrollView和subView设置了userInteractionEnableYES - MaTTP
2个回答

0

我发现在简单视图(UIViewController的一部分)中,即使 scrollView.userInteractionEnabled = true (不管 scrollView.contentSize),UIScrollView 也无法响应触摸事件。在一个干净的单视图项目中。

这个问题并不完全与上面的问题相同,但我发现我的滚动视图对于滚动触摸有反应,但没有对于普通触摸。

因此,我决定子类化滚动视图并添加协议如下:

protocol XXScrollViewDelegate {
    func scrollViewDidReportTouchesBegan()
}
class XXScrollView: UIScrollView {  
    var touchDelegate: XXScrollViewDelegate?    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.touchDelegate?.scrollViewDidReportTouchesBegan()
    }
}

不要忘记在主视图中声明协议,并设置代理(请注意,我使用了不同的名称以避免覆盖UIScrollView的代理属性)。
class ViewController: UIViewController, XXScrollViewDelegate {

scrollView.isUserInteractionEnabled = true
scrollView.touchDelegate = self

这解决了我的问题,但我仍然好奇它为什么没有按预期响应。


0

试试这些,

scrollView.userInteractionEnabled=YES;

内容大小应该大于scrollView框架大小...因此设置内容大小更大...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接