UIScrollView delaysContentTouches

半年不动的代码产品说有问题,原因是 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 万事大吉。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容