我有一个 UIView
有 touchesBegan: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/