UITableView与UITouch事件的冲突

在开发过程中会遇到在UITableView或UIScrollView上面滑动,但是会发现下面的方法不会执行,或者只会执行一次,这样就起不到应有的作用,那么怎么样才能在UITableView上面实现touch的事件呢,比如一个UIBezierPath路劲跟随滑动的距离改变。

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;

第一步:我们先创建一个继承于UITableView的子类TouchTableView(这里是继承,不是分类)

第二步:在TouchTableView.h中实现下面代码:

声明代理:

@protocol TouchTableViewDelegate<NSObject>

@optional
- (void)tableView:(UITableView *)tableView
     touchesBegan:(NSSet *)touches
        withEvent:(UIEvent *)event;

- (void)tableView:(UITableView *)tableView
 touchesCancelled:(NSSet *)touches
        withEvent:(UIEvent *)event;

- (void)tableView:(UITableView *)tableView
     touchesEnded:(NSSet *)touches
        withEvent:(UIEvent *)event;

- (void)tableView:(UITableView *)tableView
     touchesMoved:(NSSet *)touches
        withEvent:(UIEvent *)event;

@end

声明属性:

@interface TouchTableView : UITableView
{
@private __weak id _touchDelegate;

}
/*注释*/
@property (nonatomic,weak) id<TouchTableViewDelegate> touchDelegate;
@end

接下来在TouchTableView.m文件中,实现UITouch相关的方法,如下面这些方法

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;

在这些方法里面调用代理方法,

@synthesize touchDelegate = _touchDelegate;

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] && [_touchDelegate respondsToSelector:@selector(tableView:touchesBegan:withEvent:)]) {
        [_touchDelegate tableView:self touchesBegan:touches withEvent:event];
    }
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesMoved:touches withEvent:event];
    if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] && [_touchDelegate respondsToSelector:@selector(tableView:touchesMoved:withEvent:)]) {
        [_touchDelegate tableView:self touchesMoved:touches withEvent:event];
    }
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesCancelled:touches withEvent:event];
    if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] && [_touchDelegate respondsToSelector:@selector(tableView:touchesCancelled:withEvent:)]) {
        [_touchDelegate tableView:self touchesCancelled:touches withEvent:event];
    }
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    if ([_touchDelegate conformsToProtocol:@protocol(TouchTableViewDelegate)] && [_touchDelegate respondsToSelector:@selector(tableView:touchesEnded:withEvent:)]) {
        [_touchDelegate tableView:self touchesEnded:touches withEvent:event];
    }
}

第三步:在viewController里先创建个TouchTableView的属性tableView,设置代理tableView.touchDelegate = self;

/*注释*/
@property (nonatomic,strong) TouchTableView *tableView;
/*懒加载*/
- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[TouchTableView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.touchDelegate = self;
    }
    return _tableView;
}

实现代理:

- (void)tableView:(UITableView *)tableView touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    nslog(@"began");
}

- (void)tableView:(UITableView *)tableView touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        nslog(@"moved");
}

- (void)tableView:(UITableView *)tableView touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
     nslog(@"touch end");
}

注意

这里最好在touchesBegan的时候,让

self.tableView.scrollEnable = false;

然后在touchesEnd的时候,让

self.tableView.scrollEnable = true;

这么做的原因是为了避免在想要实现滑动效果的时候,会被uitableview的滚动个阻断掉,达不到预期效果


总结的就是这么多,有问题欢迎咨询;

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

相关阅读更多精彩内容

  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 11,030评论 3赞 3
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,403评论 3赞 38
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,892评论 1赞 32
  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 30,484评论 8赞 265
  • “砰!” “對不起!對不起!”女孩慌亂地收拾地上的課本,不住地道歉。 “唔!”一頭栗色碎短髮的男孩正用手捂著左眼。...
    爱砂Elsa阅读 497评论 1赞 2

友情链接更多精彩内容