首页 热点专区 小学知识 中学知识 出国留学 考研考公
您的当前位置:首页正文

UIScrollView delaysContentTouche

来源:要发发知识网

半年不动的代码产品说有问题,原因是 UISlider 滑动不灵敏,要手指先触摸一下,按上去才能滑动。场景是 <code>UISlider</code> 添加在了<code>UITableViewCell</code> 上。

因为项目中是继承了 <code>UISlider</code> ,所以,很简单的解决方案,在项目中,把UISlider 从新生成一个,放在一个什么事件都不处理的 <code>UIView</code> 上。看看效果。结果很流畅。所以问题肯定出在<code>UITableView</code> 或者 <code>UITableViewCell</code> 上。

查看UITableView 以及 UIScrollView 的文档。

@property(nonatomic) BOOL delaysContentTouches;       // default is YES. if NO, we immediately call -touchesShouldBegin:withEvent:inContentView:. this has no effect on presses

发现 delaysContentTouches 属性。默认为YES, 如果设置为NO ,会立即响应 touchesShouldBegin 方法。。。

解决方案

_tableView.delaysContentTouches = NO;
        [_tableView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[UIScrollView class]]) {
                UIScrollView *_s = (UIScrollView *)obj;
                _s.delaysContentTouches = NO;
            }
        }];


关闭掉<code>tableview</code> 所有的 <code>delaysContentTouches</code> 方法。

后来测试说,在iOS7上还是不灵敏。。嗯哼?经过测试iOS7 UITableViewCell 上的scrollView 也会开启,所以在创建cell 的时候,用同样的手段,,

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setup];
        ///#FIX iOS7 滑动不灵敏
        for (UIView *view in self.subviews) {
            if([view isKindOfClass:[UIScrollView class]]) {
                ((UIScrollView *)view).delaysContentTouches = NO;
                break;
            }
        }
    }
    return self;
}

OK 万事大吉。。。

显示全文