在cocos2d游戏中添加一个放大镜

我想在cocos2d游戏中添加一个放大镜。 这是我在网上找到的: http : //coffeeshopped.com/2010/03/a-simpler-magnifying-glass-loupe-view-for-the-iphone我已经改变了一下代码:(因为我不知道我想让放大镜跟随我们的触摸)

- (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:magnifier_rect])) { // make the circle-shape outline with a nice border. self.layer.borderColor = [[UIColor lightGrayColor] CGColor]; self.layer.borderWidth = 3; self.layer.cornerRadius = 250; self.layer.masksToBounds = YES; touchPoint = CGPointMake(CGRectGetMidX(magnifier_rect), CGRectGetMidY(magnifier_rect)); } return self; } 

然后我想在我的一个场景init方法中添加它:

  loop = [[MagnifierView alloc] init]; [loop setNeedsDisplay]; loop.viewToMagnify = [CCDirector sharedDirector].openGLView; [[CCDirector sharedDirector].openGLView.superview addSubview:loop]; 

但结果是:放大镜内的区域是黑色的。 此放大镜只是放大相同比例的图像,我怎样才能更改它以放大更靠近中心而不是靠近边缘? (就像真正的放大镜一样)谢谢!!!

在这里,我假设你想要放大屏幕的中心。

您必须根据应用需求动态更改动态大小属性。

 CGSize size = [[CCDirector sharedDirector] winSize]; id lens = [CCLens3D actionWithPosition:ccp(size.width/2,size.height/2) radius:240 grid:ccg(15,10) duration:0.0f]; [self runAction:lens]; 

Cocos2d使用OpenGL绘制,而不是CoreAnimation / Quartz。 您正在绘制的CALayer是空的,所以您什么也看不见。 您将需要使用OpenGL图形代码来执行放大镜效果或对像素进行采样并相应地更改它们以实现放大效果,如您在链接到的文章中引用的Christmann文章中所做的那样。 该代码还依赖于CoreAnimation / Quartz,因此您需要找到另一种方法来获取您希望放大的图像数据。