ios - touchesBegan :withEvent: on UIView in UITableViewCell called with noticable delay

标签 ios objective-c uitableview uiview touch

我有一个 UIViewtouchesBegan:withEvent:为了处理触摸,这个 UIView 在 UITableViewCell .

当我触摸它时,什么也没有发生,当我“长按”它时,触摸事件就会被触发。

有没有我跳过的设置?我该怎么做才能立即对触摸我的 UIView 使用react?

这是代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self];
    [self handleTouchAtLocation:touchLocation];
}

当我把 NSLog 放到这个方法中时,这是被跟踪的(格式是我的):
touchesBegan:touches withEvent:<UITouchesEvent: 0x170078f00> 
timestamp: 281850 touches: {(
    <UITouch: 0x1781486c0> 
        phase: Began tap 
        count: 1 window: <UIWindow: 0x145e0ab30; 
                    frame = (0 0; 320 568); 
                    gestureRecognizers = <NSArray: 0x178240720>; 
                    layer = <UIWindowLayer: 0x17802b800>> 
        view: <CSRateView: 0x145d49850; 
            frame = (50 54; 220 40); 
            autoresize = TM; 
            layer = <CALayer: 0x17003c0c0>> 
        location in window: {207, 81} 
        previous location in window: {207, 81} 
        location in view: {157, 7} 
        previous location in view: {157, 7}

)}

最佳答案

禁用 delaysContentTouches对于 TableView self.tableView.delaysContentTouches = NO;
然后,在你的单元格中:

@implementation MyCell

- (void)awakeFromNib
{
    for (id view in self.subviews) {
        if ([view respondsToSelector:@selector(setDelaysContentTouches:)]){
            [view setDelaysContentTouches:NO];
        }
    }
}

@end

- 编辑 -

或者在 UITableViewCell 上创建一个类别
#import "UITableViewCell+DelayTouches.h"

@implementation UITableViewCell (DelayTouches)

- (void)setDelayContentTouches:(BOOL)enable
{
    for (id view in self.subviews) {
        if ([view respondsToSelector:@selector(setDelaysContentTouches:)]){
            [view setDelaysContentTouches:enable];
        }
    }
}

@end

cellForRow:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = ...
    [cell setDelayContentTouches:NO];
    return cell;
}

关于ios - touchesBegan :withEvent: on UIView in UITableViewCell called with noticable delay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24821985/

相关文章:

ios - 如何在 titleForHeaderInSection 方法中更改字体样式和背景颜色

ios - 点击后退按钮后隐藏 UINavigationBar

ios - 如何在 S3 PutObjectRequest 完成时添加监听器?

iOS 从 YouTube 获取视频的 JSON 列表

ios - UIWebView 不要弹跳和滚动

ios - 自定义按钮点击在表页脚 View 中的 UIView 中不起作用

ios - 获取不同部分中选定单元格的列表

ios - 在 PFLogInViewController 上解析 iOS 隐藏状态栏

ios - 滚动 tableView 时最小化导航栏

ios - 缺少 Windows 选项的远程 iOS 模拟器?