我正在开发一个必须显示 50 到 70 张图像( Sprite )的应用程序。我制作了一个可以滚动所有这些图像的滚动条,但我也想缩放这些图像,我一直在关注 "http://ganbarugames.com/2010/12/detecting-touch-events-in-cocos2d-iphone/ " 本教程。我想要完全相同的东西,但那个教程只对单个 Sprite 进行缩放,但我希望在每个 Sprite 上都这样,我应该怎么做?请帮帮我好吗?
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchPoint = [touch locationInView:[touch view]];
touchPoint = [[CCDirector sharedDirector]convertToGL:touchPoint];
self.position = ccp((-(currentScreen-1)*scrollWidth)+(touchPoint.x-startSwipe),0);
--> NSArray *touchArray = [touches allObjects];
if([touchArray count] > 1)
{
UITouch *fingerOne = [touchArray objectAtIndex:0];
UITouch *fingerTwo = [touchArray objectAtIndex:1];
CGPoint pointOne = [fingerOne locationInView:[fingerOne view]];
CGPoint pointTwo = [fingerTwo locationInView:[fingerTwo view]];
pointOne = [[CCDirector sharedDirector] convertToGL:pointOne];
pointTwo = [[CCDirector sharedDirector] convertToGL:pointTwo];
float distance = sqrt(pow(pointOne.x - pointTwo.x, 2.0) + pow(pointOne.y - pointTwo.y, 2.0));
float scale = distance / [CCDirector sharedDirector].winSize.width * 5;
[backgroundScroll setScale:scale];
}
} 这是我正在使用的代码,但它在代码中提到的箭头处警告我“UITouch”可能不会响应 -allObjects”
请帮帮我..........
最佳答案
终于找到解决方案了
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(makepinch:)];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:pinch];
把他的代码放在你的init方法里然后加上
-(void)makepinch:(UIPinchGestureRecognizer*)pinch
{
if(pinch.state == UIGestureRecognizerStateEnded)
{
currentScale = pinch.scale;
}
else if(pinch.state == UIGestureRecognizerStateBegan && currentScale != 0.0f)
{
pinch.scale = currentScale;
}
if(pinch.scale != NAN && pinch.scale != 0.0)
{
pinch.view.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
}
}
关于cocos2d-iphone - 在cocos2d中捏合时如何缩放 Sprite ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5551223/