以下也只是轉貼該作者的程式碼,以防這個連結掰了=A=“
首先是要建立一個CCUIViewWrapper的File。
在CCUIViewWrapper.h內寫上:
#import "cocos2d.h"
#import
@interface CCUIViewWrapper : CCSprite
{
UIView *uiItem;
float rotation;
}
@property (nonatomic, retain) UIView *uiItem;
+ (id) wrapperForUIView:(UIView*)ui;
- (id) initForUIView:(UIView*)ui;
- (void) updateUIViewTransform;
@end
再來是在CCUIViewWrapper.m內寫上:
#import "CCUIViewWrapper.h"
@implementation CCUIViewWrapper
@synthesize uiItem;
+ (id) wrapperForUIView:(UIView*)ui
{
return [[[self alloc] initForUIView:ui] autorelease];
}
- (id) initForUIView:(UIView*)ui
{
if((self = [self init]))
{
self.uiItem = ui;
return self;
}
return nil;
}
- (void) dealloc
{
self.uiItem = nil;
[super dealloc];
}
- (void) setParent:(CCNode *)parent
{
if(parent == nil)
[uiItem removeFromSuperview];
else if(uiItem != nil)
[[[[CCDirector sharedDirector] openGLView] window] addSubview: uiItem];
[super setParent:parent];
}
- (void) updateUIViewTransform
{
float thisAngle, pAngle;
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, [[UIScreen mainScreen] bounds].size.height);
for(CCNode *p = self; p != nil; p = p.parent)
{
thisAngle = CC_DEGREES_TO_RADIANS(p.rotation);
if(!p.isRelativeAnchorPoint)
transform = CGAffineTransformTranslate(transform, p.anchorPointInPixels.x, p.anchorPointInPixels.y);
if(p.parent != nil)
{
pAngle = CC_DEGREES_TO_RADIANS(p.parent.rotation);
transform = CGAffineTransformTranslate(transform,
(p.position.x * cosf(pAngle)) + (p.position.y * sinf(pAngle)),
(-p.position.y * cosf(pAngle)) + (p.position.x * sinf(pAngle)));
}
else
transform = CGAffineTransformTranslate(transform, p.position.x, -p.position.y);
transform = CGAffineTransformRotate(transform, thisAngle);
transform = CGAffineTransformScale(transform, p.scaleX, p.scaleY);
transform = CGAffineTransformTranslate(transform, -p.anchorPointInPixels.x, -p.anchorPointInPixels.y);
}
[uiItem setTransform:transform];
}
- (void) setVisible:(BOOL)v
{
[super setVisible:v];
[uiItem setHidden:!v];
}
- (void) setRotation:(float)protation
{
[super setRotation:protation];
[self updateUIViewTransform];
}
- (void) setScaleX:(float)sx
{
[super setScaleX:sx];
[self updateUIViewTransform];
}
- (void) setScaleY:(float)sy
{
[super setScaleY:sy];
[self updateUIViewTransform];
}
- (void) setOpacity:(GLubyte)opacity
{
[uiItem setAlpha:opacity/255.0f];
[super setOpacity:opacity];
}
- (void) setContentSize:(CGSize)size
{
[super setContentSize:size];
uiItem.frame = CGRectMake(0, 0, self.contentSize.width, self.contentSize.height);
uiItem.bounds = CGRectMake(0, 0, self.contentSize.width, self.contentSize.height);
}
- (void) setAnchorPoint:(CGPoint)pnt
{
[super setAnchorPoint:pnt];
[self updateUIViewTransform];
}
- (void) setPosition:(CGPoint)pnt
{
[super setPosition:pnt];
[self updateUIViewTransform];
}
@end
使用範例,基本上在那串討論串已經有很多人提供了,這裡也轉載以下:
//build
UIView *myView = [[[UIView alloc] init] autorelease];
CCUIViewWrapper *wrapper = [CCUIViewWrapper wrapperForUIView:myView];
wrapper.contentSize = CGSizeMake(320, 160);
wrapper.position = ccp(64,64);
[self addChild:wrapper];
[wrapper runAction:[CCRotateTo actionWithDuration:.25f angle:180]];
wrapper.position = ccp(96,128);
wrapper.opacity = 127;
[wrapper runAction:[CCScaleBy actionWithDuration:.5f scale:.5f];
wrapper.visible = false;
// cleanup
[self removeChild:wrapper cleanup:true];
wrapper = nil;
不好意思想請問一下,如果我有一個cocos2d的專案,然後我現在已經成功把UIKit (MKMap) 加進去某個介面,但是成功把地圖加入後,sprite都只會在地圖的下面,調整Z值也沒有用,請問有什麼辦法可以在地圖上面顯示sprite呢??
回覆刪除謝謝!!!
老實說這題我也無解囧"
刪除我的圖反而是使用UIImageView進而在地圖上顯示~