【发布时间】:2011-03-28 08:08:18
【问题描述】:
您好,我正在尝试转发从 UITableView 前面的 UIView 收到的触摸。但是这样做我不能再让表格滚动了(see here)。
所以现在我正在尝试以编程方式使 tableview 滚动,我正在查看 setContentOffset:animated: 和 scrollRectToVisible:animated: 并且只有第一个成功。
这是我在子类 UITableView 的类中的代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"BEGTB");
//previousXPan = 0.0;
UITouch * touch = [touches anyObject];
CGPoint tPoint = [touch locationInView:self];
previousYPan = tPoint.y;
[super touchesBegan:touches withEvent:event];
//
//[super touchesShouldBegin:touches withEvent:event inContentView:self];
//[[self nextResponder] touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch * touch = [touches anyObject];
CGPoint tPoint = [touch locationInView:self];
NSLog(@"MOVTB");
NSLog(@"BOUNDZ %f, %f, %f, %f", self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.height, self.bounds.size.width);
NSLog(@"CONTENT SIZE %f, %f", self.contentSize.height, self.contentSize.width);
CGRect rc = [self bounds];
NSLog(@"%f %f", rc.size.height, rc.size.width);
CGRect newRect = CGRectMake(rc.origin.x, rc.origin.y + self.contentSize.height, self.contentSize.width, fabs(tPoint.y - previousYPan));
NSLog(@"BOND RECT %f, %f, %f, %f", rc.origin.x, rc.origin.y, rc.size.height, rc.size.width);
NSLog(@"NEW RECT %f, %f, %f, %f", newRect.origin.x, newRect.origin.y, newRect.size.height, newRect.size.width);
//[self scrollRectToVisible:newRect animated:YES];
NSLog(@"Content OFFSET %f",tPoint.y - previousYPan);
[self setContentOffset:CGPointMake(rc.origin.x,(tPoint.y - previousYPan) *2.0 )animated:YES];
previousYPan = tPoint.y;
//[super touchesMoved:touches withEvent:event];
//[[self nextResponder] touchesMoved:touches withEvent:event];
}
但结果确实很慢而且有问题。有更好的主意吗?
【问题讨论】:
-
您的 UIView 是否在 uitableviewcell 中,只需查看特定单元格内部并向前触摸即可更轻松?
-
@vodkhang:我的 UIView 是 tableview 的视图 op 顶部,它将触摸转发到 uitableviewcell 触摸,然后转发到 uitable 视图(代码在消息中的链接上)。我正在放置这个 UIView 而不是直接使用 UITableViewCell,因为我想创建一个 ImageView 以在所有内容区域中拖动。
-
这听起来可能很愚蠢,但 NSLOG 确实会消耗大量性能(尤其是在诸如 cellForRowAtIndexPath 之类的常用方法中使用时)。尝试删除所有日志调用,看看是否更好,然后再深入研究。
-
@samsam:甚至都没有评论。性能不是这样的问题,但都是越野车和怪异,它上下弹跳,有时似乎不动,有时它像火箭一样上升
-
hmmm,那么看看 touchesMoved 被触发的频率可能会有所帮助,并且可能会限制对 setContentOffset 的调用,或者至少找到一种方法让 setContentOffset 在再次调用 setcontentOffset 之前一直进行动画处理(或者也许用动画尝试一下:NO)这是我目前能想到的。干杯!
标签: iphone uitableview scroll uiscrollview touch