UIScrollView没有响应触摸

时间:2012-09-04 20:24:57

标签: cocoa-touch ipad uiscrollview touches

我有一个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,但它仍然不会滚动。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

试试这些,

scrollView.userInteractionEnabled=YES;

内容大小应大于scrollView框架大小... 所以设置内容大小更大......

答案 1 :(得分:0)

我还在一个简单视图(UIScrollView的一部分)内找到UIViewController,但没有响应触摸事件,即使使用scrollView.userInteractionEnabled = true(无论scrollView.contentSize如何)。在一个干净的单视图项目中

这个问题与上面的问题不完全相同,但我发现看到我的scrollView响应滚动触摸很有趣,但不是简单的触摸。

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

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

这解决了我的问题,但我仍然对它没有按预期做出回应的原因感到好奇。

相关问题