【问题标题】:What is this drag effect called and necessary actions to preform it?这种拖曳效果叫什么以及执行它的必要动作是什么?
【发布时间】:2014-06-17 04:18:48
【问题描述】:

嗯,我现在有点卡在寻找资源来执行这种拖动效果。 效果是这样的:

  • 触摸 SKSpriteNode 并按住
  • 然后拖动到要移动 SKSpriteNode 的位置
  • 最后,您松开手势,节点将沿直线(以直线运动)移动到您希望以延迟速度移动的位置,因此当您松开手指时,您可以看到它移动。

【问题讨论】:

  • 看看这篇文章 -- stackoverflow.com/questions/23436988/… -- 它可能有一些你需要的额外信息
  • 我可能不对,但是您可以使用 UILongPressGestures 来激活 UIPanGesture,它可以帮助您移动对象
  • 这可能会有所帮助.. raywenderlich.com/44270/…
  • Amar 这并不完全是我正在寻找的更多是将一条不可见的线拖到一个位置并释放,然后对象会转到您想去的位置。不过谢谢你的建议。

标签: ios objective-c sprite-kit touchesmoved skspritenode


【解决方案1】:

不知道拖动效果的名称,但实现起来很简单。以下是一个非常基本的实现,可以进行增强。

首先,在你的场景中声明两个实例变量

SKNode *selectedNode;
UITouch *currentTouch;

然后,您可以使用触摸代理实现所需的功能。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    CGPoint touchPoint = [touch locationInNode:self];

    SKNode *node = [self nodeAtPoint:touchPoint];

    if ([node.name isEqualToString:@"whatevername"])
    {
        selectedNode = node;
        currentTouch = touch;
    }
}



-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    if ([touch isEqual:currentTouch])
    {
        CGPoint point = [touch locationInNode:self];
        [selectedNode runAction:[SKAction moveTo:point duration:1.0]];
        //Also can calculate time based on distance to make the movement uniform.

        selectedNode = nil;
        currentTouch = nil;
    }
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    if ([touch isEqual:currentTouch])
    {
        selectedNode = nil;
        currentTouch = nil;
    }

}

【讨论】:

  • 它看起来像是要去一个随机的位置,你不能把它拖到你触摸的地方 end.kind 像这样 [link] (youtube.com/watch?v=EcdClbW-_fE) 但只有一个 Sprite Image另一个堆叠在顶部,上面没有图像,因此当您拖动它并触摸结束时,它会转到您上次触摸的位置。释放后触摸事件。如果这有意义的话。
  • 从视频来看,您似乎想让一个底层精灵跟随被拖动的精灵。
  • 是的,但更多的是拖到目标点,释放,然后使用您拥有的延迟移动代码移动到目的地。
猜你喜欢
  • 1970-01-01
  • 2018-01-30
  • 2015-11-16
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多