Iphone UITableView:以编程方式滚动内容视图
嗨,我正在尝试转发我从UITableView前面的UIView收到的触摸。但是这样我就不能再让桌子滚动了() 因此,现在我正试图通过编程方式使tableview滚动,我正在研究Iphone UITableView:以编程方式滚动内容视图,iphone,uitableview,scroll,uiscrollview,touch,Iphone,Uitableview,Scroll,Uiscrollview,Touch,嗨,我正在尝试转发我从UITableView前面的UIView收到的触摸。但是这样我就不能再让桌子滚动了() 因此,现在我正试图通过编程方式使tableview滚动,我正在研究setContentOffset:animated:和scrollRectToVisible:animated:,并且只在第一个方面取得了成功。 下面是我在UITableView子类中的代码: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
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];
}
但结果真的很落后,很糟糕。有更好的主意吗?我几乎找到了一个合适的解决方案(它不像普通的滚动,而是…) 首先,我确定Y轴上表格视图内容区域的最大和最小像素量(视图完全加载时contentSize的高度) 我检查的动作增量完成,但不参考触摸在表视图中的位置!我必须在覆盖它的顶层UIView上检查它,因为当你改变表的内容偏移量时,你会动态地改变它的维度,最终你会产生一种加速效果 下面是一些代码:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch * touch = [touches anyObject];
CGPoint tPoint = [touch locationInView:[viewController draggableAreaView]];
CGRect rc = [self bounds];
float totalOS = tPoint.y - previousYPan;
if ((contentOffSetY >= minY && contentOffSetY <= maxY) ||
(contentOffSetY < minY - 5 && totalOS < 0) ||
(contentOffSetY > maxY + 5 && totalOS > 0))
{
if (totalOS > 0)
totalOS += OFFSET;
else if (totalOS < 0)
totalOS -= OFFSET;
[self setContentOffset:CGPointMake(rc.origin.x,self.contentOffset.y + totalOS)animated:NO];
}
else /*if (contentOffSetY < minY - 5 && totalOS < 0) */
{
[self resetOffset];
}
previousYPan = tPoint.y;
}
- (void)resetOffset{
CGRect rc = [self bounds];
if(contentOffSetY < minY)
{
[self setContentOffset:CGPointMake(rc.origin.x, minY + 50)animated:YES];
}
else if (contentOffSetY > maxY)
{
[self setContentOffset:CGPointMake(rc.origin.x, maxY - 50)animated:YES];
}
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件{
UITouch*touch=[触摸任何对象];
CGPoint tPoint=[touch locationInView:[viewController draggableAreaView]];
CGRect rc=[自边界];
float totalOS=tPoint.y-上一个pan;
如果((contentOffSetY>=minY&&contentOffSetY maxY+5&&totalOS>0))
{
如果(totalOS>0)
totalOS+=偏移量;
否则如果(totalOS<0)
totalOS-=偏移量;
[self-setContentOffset:CGPointMake(rc.origin.x,self.contentOffset.y+totalOS)动画:否];
}
else/*if(contentOffSetYmaxY)
{
[self-setContentOffset:CGPointMake(rc.origin.x,maxY-50)动画:是];
}
}
更多提示:
- 这里的DragableAreaView是我获得“绝对”位置的俯视图
- 偏移量是一个常数,定义为线性增加滚动速度(没有它,
,多次调用,每次仅给出一个像素的相对移动)触摸移动:
- ,
和minY
是预先计算的极限值(为了计算它们,我计算了表中所有单元格的高度(max)和可见内容视图的高度(min)maxY
- 我在
和minY
中添加了一些位移,以获得更清晰的动画效果maxY