<分区>
我正在尝试使用 Xcode 为 iPhone 应用程序创建效果,但不确定它叫什么。
我会尽我所能描述它:
- 目标是将球滑过屏幕以击中目标
- 用户不应该一直拖动它,因为那样太容易了
- 球应该只在用户松开触摸后开始移动
- 用户“滑动”物体的速度越快,物体移动的速度就越快
这是我可以在 Xcode 中使用的现有效果吗?
谢谢。
标签 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/