gpt4 book ai didi

ios - 平滑我的滚动 TouchesMoved

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:00:34 25 4
gpt4 key购买 nike

我有一个位于表格 View 上方的半透明 View 。当我将我的 View 拖到屏幕底部附近时,我想平滑地滚动它下面的表格 View 。

我正在尝试模仿:

用户将悬停 View 拖到屏幕底部附近并将其固定在那里。表格 View 滚动直到他们松开或到达表格底部。用户松开悬停 View 。

目前我在做:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (!_hoverViewTouch) {
return;
}

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
CGPoint prevLocation = [touch previousLocationInView:self];

//scroll down.
if (prevLocation.y < location.y) {
if (location.y >= _table.frame.size.height - rowHeight) {
NSArray *cells = [_table visibleCells];
UITableViewCell *cell = [cells objectAtIndex:round(cells.count / 2)];
NSIndexPath *indexPath = [_table indexPathForCell:cell];
[_table scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:true];
}
}

CGRect frame = [hoverView frame];
frame.origin.y = location.y - (hoverView.frame.size.height / 2.0f);
[hoverView setFrame:frame];
}

但是,它滚动到表格底部的速度如此之快。我怎样才能减慢或平滑滚动?

最佳答案

UITableView 的 scrollToRowAtIndexPath 方法无法让您控制动画速度。相反,您可以使用 CADisplayLink 来平滑更新 TableView 的 contentOffset

- (void)startAnimatingTable
{
self.scrollStartDate = [NSDate date];
self.startContentOffset = self.tableView.contentOffset;
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkFired:)];
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)stopAnimatingTable
{
[self.displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
self.displayLink = nil;
}

- (void)displayLinkFired:(CADisplayLink *)displayLink
{
NSTimeInterval interval = -[self.scrollStartDate timeIntervalSinceNow];
CGFloat speed = 100;
CGFloat yOffset = self.startContentOffset.y + speed * interval;
self.tableView.contentOffset = CGPointMake(self.startContentOffset.x, yOffset);
}

关于ios - 平滑我的滚动 TouchesMoved,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31226479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com