我有一个需要拖动和模拟惯性滚动的对象。
到目前为止,这是我工作缓慢的情况。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];
self.lastTouch = touchLocation;
self.lastTimestamp = event.timestamp;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInNode:self];
// how much it scrolled vertically (it is a table view, no need to scroll horizontally)
CGFloat deltaY = currentLocation.y - self.lastTouch.y;
// move the container (that is the object I want to implement the inertial movement)
// to the correct position
CGPoint posActual = self.container.position;
posActual.y = posActual.y + deltaY;
[self.container setPosition:posActual];
// calculate the movement speed
NSTimeInterval deltaTime = event.timestamp - self.lastTimestamp;
self.speedY = deltaY / deltaTime;
self.lastTouch = currentLocation;
self.lastTimestamp = event.timestamp;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGFloat tempoDecay = 0.4f;
CGPoint finalPosition = self.container.position;
finalPosition.y = finalPosition.y + (self.speedY * tempoDecay);
// move the object to the final position using easeOut timing...
}
这就是我所看到的:我刷它。当我抬起手指时,它会加速,然后突然停止。我已经记录了 speedY 值,这些值非常大,比如 720!(每秒 720 像素?)
我不能使用 Apple 提供的 UIScrollView 或其他方法。它是一个必须靠自身惯性滚动的对象。谢谢。