ios - 这个效果叫什么?

标签 ios objective-c

<分区>

我正在尝试使用 Xcode 为 iPhone 应用程序创建效果,但不确定它叫什么。

我会尽我所能描述它:

  • 目标是将球滑过屏幕以击中目标
  • 用户不应该一直拖动它,因为那样太容易了
  • 球应该只在用户松开触摸后开始移动
  • 用户“滑动”物体的速度越快,物体移动的速度就越快

这是我可以在 Xcode 中使用的现有效果吗?

谢谢。

最佳答案

问:

Is this an existing effect that I can use in Xcode?

答:没有

然而,一种方法是从“轻弹”手势创建一个CGVector。然后将该矢量作为插入力应用到您正在“轻弹”的对象上。以下是与 SpriteKit 一起使用的,但是其他框架可以采用从“flick”手势获取矢量的原则。

#define FLICK_SCALAR 0.5 // tweak this to alter sensitivity

我们需要一个辅助结构 -

typedef struct TouchData {
CGPoint point;
NSTimeInterval time;
} TouchData;

还有一个属性——

@property TouchData touchOriginData;

// Touch Handling
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // store the start data
    for (UITouch *touch in touches) {
        _touchOriginData.point = [touch locationInNode:self];
        _touchOriginData.time = [touch timestamp];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    // store the end data
    TouchData touchEndData;
    for (UITouch *touch in touches) {
        touchEndData.point = [touch locationInNode:self];
        touchEndData.time = touch.timestamp;
    }
    // calculate the impulse vector from TouchData structs
    NSTimeInterval timeTaken = touchEndData.time - _touchOriginData.time;
    CGFloat vector_x = (touchEndData.point.x - _touchOriginData.point.x) / timeTaken * FLICK_SCALAR;
    CGFloat vector_y = (touchEndData.point.y - _touchOriginData.point.y) / timeTaken * FLICK_SCALAR;
    CGVector impulseVector = CGVectorMake(vector_x, vector_y);
    // fire projectile node
    [self someMethodThatFiresWithVector:impulseVector];
}

关于ios - 这个效果叫什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23177296/

相关文章:

objective-c - Objective-C中的id类型真的是动态的吗

ios - 一秒钟后 UIButton 退出突出显示模式

ios - 更新 Metal 中 MTLBuffer 的内容

iOS 编程菜鸟,需要帮助修复错误消息

ios - 使用 Storyboard 更改 segue 端点

javascript - UIWebView 将用户名和密码传递到表单中

ios - UITableView didDeselectRowAtIndexPath Cell 在未选中时返回 Not on Selected

ios - 如何以编程方式区分 iOS 中的公共(public) API 与未记录/私有(private) API?

ios - Firebase - 读取和获取数据的效率

ios - Material Design 的 ButtomSheet 无法正确显示 - swift