use*_*894 2 objective-c cocos2d-iphone
Cocos2d:你好.很长一段时间试图通过缩放捏,但并非不可能.你能告诉我们如何实现缩放.
这是我在我的一个游戏中用于缩放缩放的代码.
首先将@property添加到要剪切缩放的场景的@interface(或更可能的图层):
@property (nonatomic, strong) NSMutableSet * touches;
Run Code Online (Sandbox Code Playgroud)
这里是您可以添加到图层的@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;
}
Run Code Online (Sandbox Code Playgroud)
请注意,我已经复制了大部分代码,删除了无关的逻辑并且没有启动此代码.如果您对此代码有任何问题,请与我们联系.
| 归档时间: |
|
| 查看次数: |
1408 次 |
| 最近记录: |