baz*_*ola 4 zoom objective-c pinch ios sprite-kit
我已经在这段代码上工作了很长一段时间,但它只是感觉向前迈出了一步,后退了两步.我希望有人可以帮助我.
我正在使用Sprite Kit,所以我有一个Scene文件来管理渲染,UI和触摸控件.我有一个像这样的相机功能的SKNode:
_world = [[SKNode alloc] init];
[_world setName:@"world"];
[self addChild:_world];
Run Code Online (Sandbox Code Playgroud)
我正在使用UIGestureRecognizer,所以我添加了我需要的那些:
_panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanFrom:)];
[[self view] addGestureRecognizer:_panRecognizer];
_pinchRecognizer = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
[[self view] addGestureRecognizer:_pinchRecognizer];
Run Code Online (Sandbox Code Playgroud)
平移工作正常,但不是很好.捏是真正的问题.捏的想法是抓住屏幕中心的一个点,将该点转换为世界节点,然后在放大时移动到它.这是捏的方法:
-(void) handlePinch:(UIPinchGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
_tempScale = [sender scale];
}
if (sender.state == UIGestureRecognizerStateChanged) {
if([sender scale] > _tempScale) {
if (_world.xScale < 6) {
//_world.xScale += 0.05;
//_world.yScale += 0.05;
//[_world setScale:[sender scale]];
[_world setScale:_world.xScale += 0.05];
CGPoint screenCenter = CGPointMake(_initialScreenSize.width/2, _initialScreenSize.height/2);
CGPoint newWorldPoint = [self convertTouchPointToWorld:screenCenter];
//crazy method why does this work
CGPoint alteredWorldCenter = CGPointMake(((newWorldPoint.x*_world.xScale)*-1), (newWorldPoint.y*_world.yScale)*-1);
//why does the duration have to be exactly 0.3 to work
SKAction *moveToCenter = [SKAction moveTo:alteredWorldCenter duration:0.3];
[_world runAction:moveToCenter];
}
} else if ([sender scale] < _tempScale) {
if (_world.xScale > 0.5 && _world.xScale > 0.3){
//_world.xScale -= 0.05;
//_world.yScale -= 0.05;
//[_world setScale:[sender scale]];
[_world setScale:_world.xScale -= 0.05];
CGPoint screenCenter = CGPointMake(_initialScreenSize.width/2, _initialScreenSize.height/2);
CGPoint newWorldPoint = [self convertTouchPointToWorld:screenCenter];
//crazy method why does this work
CGPoint alteredWorldCenter = CGPointMake(((newWorldPoint.x*_world.xScale - _initialScreenSize.width)*-1), (newWorldPoint.y*_world.yScale - _initialScreenSize.height)*-1);
SKAction *moveToCenter = [SKAction moveTo:alteredWorldCenter duration:0.3];
[_world runAction:moveToCenter];
}
}
}
if (sender.state == UIGestureRecognizerStateEnded) {
[_world removeAllActions];
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了很多这样的迭代,但是这个确切的代码让我最接近于捏住世界上的一个点.但是有一些问题.当你从中心越来越远时,它也不能正常工作,因为它几乎仍然试图放大世界的中心.在将中心点转换为世界节点之后,我仍然需要再次操作它以使其正确居中(我描述的公式为疯狂).放大和缩小工作时必须有所不同.移动动作的持续时间必须设置为0.3,否则几乎不起作用.更高或更低,它不会放大中心点.如果我尝试将变焦量增加一点以上,它会快速移动.如果我在收尾结束时没有结束动作,屏幕就会抖动.我不明白为什么这个有用(它在延迟结束和屏幕抖动之前平滑地放大到中心点)并且我不确定我做错了什么.任何帮助深表感谢!
看看我对一个非常相似的问题的答案.
我发布的代码"缩放"了缩放手势位置的缩放而不是屏幕的中心,但这很容易改变,因为我尝试了两种方式.
根据以下评论的要求,我也将我的平移代码添加到此答案中.
平移代码......
// instance variables of MyScene.
SKNode *_mySkNode;
UIPanGestureRecognizer *_panGestureRecognizer;
- (void)didMoveToView:(SKView *)view
{
_panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
[[self view] addGestureRecognizer:_panGestureRecognizer];
}
- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan) {
[recognizer setTranslation:CGPointZero inView:recognizer.view];
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:recognizer.view];
translation = CGPointMake(-translation.x, translation.y);
_mySkNode.position = CGPointSubtract(_mySkNode.position, translation);
[recognizer setTranslation:CGPointZero inView:recognizer.view];
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
// No code needed for panning.
}
}
Run Code Online (Sandbox Code Playgroud)
以下是上面使用的两个辅助函数.它们来自Ray Wenderlich关于Sprite Kit的书.
SKT_INLINE CGPoint CGPointAdd(CGPoint point1, CGPoint point2) {
return CGPointMake(point1.x + point2.x, point1.y + point2.y);
}
SKT_INLINE CGPoint CGPointSubtract(CGPoint point1, CGPoint point2) {
return CGPointMake(point1.x - point2.x, point1.y - point2.y);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3965 次 |
| 最近记录: |