gpt4 book ai didi

objective-c - Cocos2d :how to implement zooming by pinch

转载 作者:行者123 更新时间:2023-12-02 07:01:13 26 4
gpt4 key购买 nike

Cocos2d:你好。很长一段时间试图通过缩放捏来做,但并非不可能。能否请您告诉如何实现缩放。

最佳答案

这是我在我的一款游戏中用于缩放的代码。

首先将此@property 添加到您想要缩放的场景(或更可能的图层)的@interface 中:

@property (nonatomic, strong) NSMutableSet * touches;

这是您可以添加到层的@implementation 的代码

- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_touches unionSet:touches];
}

- (void) ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[_touches minusSet:touches];
}

- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[_touches minusSet:touches];
}

- (void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch * touch in touches)
{
[self pinchZoomWithMovedTouch:touch];
}
}

- (void) pinchZoomWithMovedTouch: (UITouch *) movedTouch
{
CGFloat minDistSqr = CGFLOAT_MAX;
UITouch * nearestTouch = nil;
UIView * mainView = [[CCDirector sharedDirector] view];
CGPoint newLocation = [movedTouch locationInView:mainView];
for (UITouch * touch in _touches)
{
if (touch != movedTouch)
{
CGFloat distSqr = sqrOfDistanceBetweenPoints([touch locationInView:mainView],newLocation);
if (distSqr < minDistSqr)
{
minDistSqr = distSqr;
nearestTouch = touch;
}
}
}
if (nearestTouch)
{
CGFloat prevDistSqr = sqrOfDistanceBetweenPoints([nearestTouch locationInView:mainView],
[movedTouch previousLocationInView:mainView]);
CGFloat pinchDiff = sqrtf(minDistSqr) - sqrtf(prevDistSqr);
self.scale += pinchDiff * kPinchZoomCoeff; // kPinchZoomCoeff is constant = 1.0 / 200.0f Adjust it for your needs
}
}

CGFloat sqrOfDistanceBetweenPoints(CGPoint p1, CGPoint p2)
{
CGPoint diff = ccpSub(p1, p2);
return diff.x * diff.x + diff.y * diff.y;
}

请注意,我已经复制粘贴了大部分代码并删除了无关逻辑并且没有启动此代码。如果您对此代码有任何问题,请告诉我。

关于objective-c - Cocos2d :how to implement zooming by pinch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20439233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com