2010-01-30 110 views
0

我有一个UITableViewController并希望检测触摸。UITableViewController和检测触摸

基本上,我希望用户能够按住触摸3秒,当他们放开时,我想弹出一个小的视图,有几个选项的表。

我已经试过这...

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
self.lastTouch = [event timestamp]; 
NSLog(@"TLC.touchesBegan:touchBeginEndInterval %f", self.lastTouch); 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
NSTimeInterval touchBeginEndInterval = [event timestamp] - self.lastTouch 
NSLog(@"TLC.touchesEnded:touchBeginEndInterval %f %f", self.lastTouch, touchBeginEndInterval); 
} 

而且这还不是捡我倒是在所有...

任何想法要实现这一目标的最佳方式是什么?

+0

你解决这个问题?如果是这样,让我知道解决方案。即使我面临同样的问题 – Dee 2012-07-17 18:34:34

回答

1

TableViews有一种“触摸”委托方法。为你的tableView代理实现这个方法:

// What happens when a row is touched 
- (void)tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)indexPath {} 

你不应该让他们按3秒...只是使用标准。

+0

我已经在使用其他的东西了。 我想让他们按住几秒钟,然后我弹出另一个视图。 我想过使用editingStyleForRowAtIndexPath方法,但想把它作为Delete(它当前是)。 有没有其他想法? – 2010-01-30 01:53:00

+0

您应该使用tableViewCell中的蓝色小按钮。这是与细胞进行更多互动的标准方式。 http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/MobileHIG/art/UIButtonTypeDetailDisclosure.jpg。此按钮在HIG中列出:http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/MobileHIG/SystemProvided/SystemProvided.html#//apple_ref/doc/uid/TP40006556-CH15-SW2 – 2010-01-30 03:32:49

0

假设代码来自您的UITableViewController子类... UIViewController s不会收到类似touchesBegan:withEvent的方法。只有UIResponder s像UIView其子类UITableView这样做。所以你试图在错误的地方得到触动。

你想完成什么?您是否想要回应UITableViewUITableViewCell?在后者的情况下,您可以处理自定义单元实现中的触摸。

+0

UIViewController继承自UIResponder,因此可以接收触摸。 请参阅http://developer.apple.com/iphone/library/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html – 2010-07-08 13:05:01

0

现在很容易与UILongPressGestureRecognizer做到:

-(void)viewDidLoad{ 
    [super viewDidLoad]; 
    UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandlerMethod:)]; 
    [self.tableView addGestureRecognizer:longPressRecognizer]; 
} 
-(void)longPressHandlerMethod:(UIGestureRecognizer *)gestureRecognizer{ 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[gestureRecognizer locationInView:self.tableView]]; 
    ... 
}