touchesBegan:withEvent:在UITable中以UITableViewCell调用,具有明显的延迟

时间:2014-07-18 09:46:44

标签: ios objective-c uitableview uiview touch

我有UIView touchesBegan:withEvent:处理触摸,此UIView位于UITableViewCell

当我触摸它时没有任何反应,当我“长按”它然后触摸事件被触发。

我跳过了设置吗?我可以做些什么来立即反应我的UIView

这是代码:

- (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}

)}

1 个答案:

答案 0 :(得分:3)

禁用tableView的delaysContentTouches 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;
}