好的,所以我找到了解决方案。
看到我正在使用 Cocos2D,在 CCLayer 上使用 UIKit 元素是困难的(即不是标准的)。因此,我必须实现 CCUIViewWrapper 并且一切正常。
这是 CCUIViewWrapper 与 Cocos2D 2.X 和 ARC 一起使用的代码;
CCUIViewWrapper.h
#import "cocos2d.h"
@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];
}
- (id) initForUIView:(UIView*)ui
{
if((self = [self init]))
{
self.uiItem = ui;
return self;
}
return nil;
}
-(void)setParent:(CCNode *)parent {
if(parent == nil) {
[uiItem removeFromSuperview];
} else if(uiItem != nil) {
[[[CCDirector sharedDirector] view] addSubview:uiItem];
}
[super setParent:parent];
}
-(void)updateUIViewTransform {
float thisAngle, pAngle;
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, [[CCDirector sharedDirector] winSize].height);
for(CCNode *p = self; p != nil; p = p.parent) {
thisAngle = CC_DEGREES_TO_RADIANS(p.rotation);
if(p.ignoreAnchorPointForPosition)
transform = CGAffineTransformTranslate(transform, p.anchorPointInPoints.x, p.anchorPointInPoints.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.anchorPointInPoints.x, -p.anchorPointInPoints.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.0];
[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
我希望这对其他人有帮助...