├── CCUIViewWrapper.h ├── CCUIViewWrapper.m └── README /CCUIViewWrapper.h: -------------------------------------------------------------------------------- 1 | /* 2 | * CCUIViewWrapper 3 | * http://www.cocos2d-iphone.org/forum/topic/6889 4 | * 5 | * Copyright (C) 2010 Blue Ether 6 | * 7 | * This program is free software; you can redistribute it and/or modify 8 | * it under the terms of the 'cocos2d for iPhone' license. 9 | * 10 | * You will find a copy of this license within the cocos2d for iPhone 11 | * distribution inside the "LICENSE" file. 12 | * 13 | */ 14 | 15 | #import "cocos2d.h" 16 | 17 | @interface CCUIViewWrapper : CCSprite 18 | { 19 | UIView *uiItem; 20 | float rotation; 21 | } 22 | 23 | @property (nonatomic, retain) UIView *uiItem; 24 | 25 | + (id)wrapperForUIView:(UIView*)ui; 26 | - (id)initForUIView:(UIView*)ui; 27 | 28 | - (void)updateUIViewTransform; 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /CCUIViewWrapper.m: -------------------------------------------------------------------------------- 1 | /* 2 | * CCUIViewWrapper 3 | * 4 | * http://www.cocos2d-iphone.org/forum/topic/6889 5 | * 6 | * Copyright (C) 2010 Blue Ether 7 | * 8 | * This program is free software; you can redistribute it and/or modify 9 | * it under the terms of the 'cocos2d for iPhone' license. 10 | * 11 | * You will find a copy of this license within the cocos2d for iPhone 12 | * distribution inside the "LICENSE" file. 13 | * 14 | */ 15 | 16 | #import "CCUIViewWrapper.h" 17 | 18 | @implementation CCUIViewWrapper 19 | 20 | @synthesize uiItem; 21 | 22 | /* -------------------------------------------------------------------------- */ 23 | + (id)wrapperForUIView:(UIView*)ui 24 | { 25 | return [[[self alloc] initForUIView:ui] autorelease]; 26 | } 27 | 28 | /* -------------------------------------------------------------------------- */ 29 | - (id)initForUIView:(UIView*)ui 30 | { 31 | self = [self init]; 32 | if (!self) 33 | return nil; 34 | 35 | self.uiItem = ui; 36 | return self; 37 | } 38 | 39 | /* -------------------------------------------------------------------------- */ 40 | - (void)dealloc 41 | { 42 | self.uiItem = nil; 43 | [super dealloc]; 44 | } 45 | 46 | /* -------------------------------------------------------------------------- */ 47 | - (void)setParent:(CCNode *)parent 48 | { 49 | if (parent == nil) 50 | [uiItem removeFromSuperview]; 51 | else if(uiItem != nil) 52 | [[[CCDirector sharedDirector] openGLView] addSubview:uiItem]; 53 | [super setParent:parent]; 54 | } 55 | 56 | /* -------------------------------------------------------------------------- */ 57 | - (void)updateUIViewTransform 58 | { 59 | float thisAngle, pAngle; 60 | CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 61 | [[UIScreen mainScreen] bounds].size.height); 62 | 63 | for (CCNode *p = self; p != nil; p = p.parent) { 64 | thisAngle = CC_DEGREES_TO_RADIANS(p.rotation); 65 | 66 | if (!p.isRelativeAnchorPoint) 67 | transform = CGAffineTransformTranslate( 68 | transform, p.anchorPointInPixels.x, p.anchorPointInPixels.y); 69 | 70 | if (p.parent != nil) { 71 | pAngle = CC_DEGREES_TO_RADIANS(p.parent.rotation); 72 | 73 | transform = CGAffineTransformTranslate(transform, 74 | (p.position.x * cosf(pAngle)) + (p.position.y * sinf(pAngle)), 75 | (-p.position.y * cosf(pAngle)) + (p.position.x * sinf(pAngle))); 76 | } else { 77 | transform = CGAffineTransformTranslate( 78 | transform, p.position.x, -p.position.y); 79 | } 80 | 81 | transform = CGAffineTransformRotate(transform, thisAngle); 82 | transform = CGAffineTransformScale(transform, p.scaleX, p.scaleY); 83 | 84 | transform = CGAffineTransformTranslate( 85 | transform, -p.anchorPointInPixels.x, -p.anchorPointInPixels.y); 86 | } 87 | 88 | [uiItem setTransform:transform]; 89 | } 90 | 91 | /* -------------------------------------------------------------------------- */ 92 | - (void)setVisible:(BOOL)v 93 | { 94 | [super setVisible:v]; 95 | [uiItem setHidden:!v]; 96 | } 97 | 98 | /* -------------------------------------------------------------------------- */ 99 | - (void)setRotation:(float)protation 100 | { 101 | [super setRotation:protation]; 102 | [self updateUIViewTransform]; 103 | } 104 | 105 | /* -------------------------------------------------------------------------- */ 106 | - (void)setScaleX:(float)sx 107 | { 108 | [super setScaleX:sx]; 109 | [self updateUIViewTransform]; 110 | } 111 | 112 | /* -------------------------------------------------------------------------- */ 113 | - (void)setScaleY:(float)sy 114 | { 115 | [super setScaleY:sy]; 116 | [self updateUIViewTransform]; 117 | } 118 | 119 | /* -------------------------------------------------------------------------- */ 120 | - (void)setOpacity:(GLubyte)opacity 121 | { 122 | [uiItem setAlpha:opacity / 255.0f]; 123 | [super setOpacity:opacity]; 124 | } 125 | 126 | /* -------------------------------------------------------------------------- */ 127 | - (void)setContentSize:(CGSize)size 128 | { 129 | [super setContentSize:size]; 130 | CGRect rect = CGRectMake(0, 0, size.width, size.height); 131 | uiItem.frame = rect; 132 | uiItem.bounds = rect; 133 | } 134 | 135 | /* -------------------------------------------------------------------------- */ 136 | - (void)setAnchorPoint:(CGPoint)pnt 137 | { 138 | [super setAnchorPoint:pnt]; 139 | [self updateUIViewTransform]; 140 | } 141 | 142 | /* -------------------------------------------------------------------------- */ 143 | - (void)setPosition:(CGPoint)pnt 144 | { 145 | [super setPosition:pnt]; 146 | [self updateUIViewTransform]; 147 | } 148 | 149 | @end 150 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | The purpose for this repository is only for easy access of CCUIViewWrapper code. 2 | 3 | It's just copy of the following thread of cocos2d forum. 4 | 5 | CCUIViewWrapper - wrapper for manipulating UIViews using Cocos2D 6 | http://www.cocos2d-iphone.org/forum/topic/6889 7 | 8 | --------------------------------------------------------------------------------