Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 模拟对象的惯性滚动_Iphone_Ios_Ipad_Cocoa Touch - Fatal编程技术网

Iphone 模拟对象的惯性滚动

Iphone 模拟对象的惯性滚动,iphone,ios,ipad,cocoa-touch,Iphone,Ios,Ipad,Cocoa Touch,我有一个对象,我需要拖动和模拟惯性滚动 这就是我到目前为止所做的工作 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint touchLocation = [touch locationInNode:self]; self.lastTouch = touchLocation; self.las

我有一个对象,我需要拖动和模拟惯性滚动

这就是我到目前为止所做的工作

- (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...

}
这就是我看到的:我刷了它。当我抬起手指时,它会加速,然后突然停止。我已经记录了快速值,这些值是巨大的,比如720!(每秒720像素?)


我不能使用UIScrollView或苹果提供的其他方法。这是一个物体,它自身必须带着惯性滚动。谢谢。

在刷卡过程中多次触摸移动通话。每次通话的两点之间的距离为15-30像素。让滑动持续0.5秒,并调用该方法10次。速度=30/0.05=600


还仅考虑了最后一次“触摸移动速度”。也许您需要计算平均速度?

我通过在场景的更新方法中添加“惯性制动”来实现这种惯性滚动(一个垂直填充了许多精灵的SKSpriteNode对象,以模拟垂直滚动选择)

// add the variables as member variables to your class , setup with initial values on your init 
  SKSpriteNode *mList // this is the sprite node that will be our scroll object
  mTouching = NO;         // BOOL
  mLastScrollDist = 0.0f; // float
在触摸开始时,将M触摸更改为是,表示触摸生效

在TouchsMoved中,计算mLastScrollDist=previousLocation.y-currentlocation.y,然后将其添加到mList的y位置(mList.y+=mLastScrollDist)

在触摸中,将“触摸”更改为“否”

最后在场景的更新方法中,计算制动惯性效应

- (void)update:(NSTimeInterval)currentTime
{
    if (!mTouching)
    {
        float slowDown = 0.98f;

        if (fabsf(mLastScrollDist) < 0.5f)
            slowDown = 0;

        mLastScrollDist *= slowDown;
        mList.y += mLastScrollDist; // mList is the SKSpriteNode list object
    }
}
-(void)更新:(NSTimeInterval)currentTime
{
如果(!mTouching)
{
浮动减速=0.98f;
if(fabsf(MLASTCROLLDIST)<0.5f)
减速=0;
MLASTCROLLDIST*=减速;
mList.y+=mLastScrollDist;//mList是SKSpriteNode列表对象
}
}

你对这个老问题有好几点意见。不要只是删除它,然后重新开始。我已经要求人们检查我的代码,而不是提供黑客建议,我可以用这些黑客来创建一个蹩脚的代码,让它通过可疑的方式工作。显然,我的要求是合理的,是可以做到的。所以,你可以试着回答我提出的问题,而不是像你最近提出的问题那样否决投票,发表没有帮助的评论。如果你没有知识或不想帮助别人,让其他人更专业地尝试。谢谢。我投了反对票,因为你删除了你的旧问题,删除了你认为没有用的评论,然后创建了一个新问题。建议的内容根本不是黑客行为,如果你按照给出的建议观看相关的WWDC视频,你会发现苹果开发团队实际上使用了相同的技术。我过去看过这两个spritekit视频,不记得看到过。听了你的建议,我擦洗了这两个视频(因为我现在没有时间花两个小时看它们),但什么也没发现。我添加了一个平均计算,但速度降低可以忽略不计。现在发生的是,应该减速的不是。事实上,当我抬起手指时,物体会加速,然后减速,然后突然停止。