How to rotate CCUIViewWrapper view with cocos2D - iphone

Using CCUIViewWrapper class in cocos2D for creating custom UITableView in my game.Problem is when game is rotating scene is rotating well but by table view is not rotating.
So , i want to know how to rotate CCUIViewWrapper view with cocos2D, perhaps in method like shouldAutorotateToInterfaceOrientation for ios less than 6 and for ios6 shouldAutorotate.

The thing is that the UIKit object isn't part of the cocos2D view, so when you rotate the cocos2D view, it is independent of the UIKit object.
I suspect that you could apply a UIView animation to the UITableView like:
[UIView animateWithDuration:0.25
animations:^{
theView.transform = CGAffineTransformMakeRotation(radians);
}];
Within the CCUIViewWrapper implementation you should be able to override the setRotation: message to apply the same rotation to the UIView it contains, and in theory they should stay in sync.
Just remember that cocos2D seems to use degrees and you'll need to transform this to radians for UIKit (or that's my recollection anyway).

Related

CCDirecter replace scene with UIView / nib

What I would like to do is have the Cocos2d shared director to replace the current scene with a UIView from UIKit (possibly loaded from a nib). The idea being to cleanly transition between Cocos2d and UIKit.
I've seen a few approaches to this problem. Most of them are about mixing UIKit with Cocos2d in the same scene, I'd like to keep them separate. One approach is to add an instance of UIView as a subview of the directors GLView. Like so:
UIView* cocosView = [[CCDirector sharedDirector] openGLView];
[cocosView addSubview:t];
Overlaying a UIView onto a Cocos layer?
This works but I would have to create a blank scene and transition to that first, then add the UIView, then later remove it and transition to whatever other scene. That's modular but a little messy.
A better approach is to wrap a UIView into a node. Something like:
http://www.cocos2d-iphone.org/forum/topic/6889
Is there an official cocos2d extension for this? I'd also like to load the UIView from storyboard/nib if possible. Much obliged.
replace the current scene with a UIView from UIKit
if you are using cocos2d 1.x: http://www.raywenderlich.com/4817/how-to-integrate-cocos2d-and-uikit
if you are using cocos2d 2.x: CCDirector is a subclass of UIViewController, so you can use usual methods. There's a little bug with delta time calculation after stopping/starting animation, but it's easy to fix.

Infinite rotating image background UIImageView

I'd like to extend a class from UIImageView,which will infinite rotate a universe image. So I can simply put this image view as background view for other UI elements.
3 questions here:
Is it the right way to extend from UIImageview class.
How to keep rotating infinite, and as background, when put it into other views, I don't need to write extra lines of code.
I wrote a rough prototype, when I put it into other views as background, all UI elements in this view are rotating with the image.
Assume that you have a UIImageView and assign an image on it and named myImgView. On viewDidLoad, add your code as bellow:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationRepeatCount:MAXFLOAT];
myImgView.transform = CGAffineTransformMakeRotation(M_PI);
[UIView commitAnimations];
It works for me like a charm.
There is nothing wrong in extending UIImageview Class.
Create rotation animation and set repeat ON.
Do not add element on rotating image. (Add elements in base view. Insert rotating imageview at index 0).
From the UIView Class Reference:
The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.
At present, UIImageView subclasses do work, especially if they don't interfere with the drawing code. Just don't complain to apple if a future iOS update breaks your subclass. It's easy enough to write your own UIView subclass to display an image. If you already have an appropriate graphics, consider just placing it in CALayer contents, instead of implementing the UIView drawRect method.
Rotations by changing UIView transform (which is of type CGAffineTransform) always take the shortest path. This means that a clockwise rotation of 270 degrees is animated as an anticlockwise rotation of 90 degrees, and any multiple of 360 degrees will do nothing. You can get 360 degree rotations if you obtain the view's layer, and animate CALayer transform (which is of type CATransform3D). See Can I use CGAffineTransformMakeRotation to rotate a view more than 360 degrees?
To make the animation repeat endlessly, the CAMediaTiming Protocol Reference recommends setting repeatCount to HUGE_VALF.

Use CCLabelBMFont inside UIViewController Inside Node

I'am doing a game, when the player lose, and screen of Game Over appears. But my Game Over screen its in a UIViewController. I call it in my Director, and alls right.
The Game over screen appears like overlay in the game
But i am using Custom Fonts, and in my Game Over screen I want to use it. But how could I use only "CCLabelBMFont" in the Game Over.
I think a way (no the better one). Its that you print in a bigger z-index than the Game Over the text I want it with "CCLabelBMFont". But I want to have all the stuff of Game Over in the GameOver.m
What could I do? Thanks to everybody.
To present anything from Cocos2D on top of a UIKit view, you'll have to make the Cocos2D view transparent and introduce a dummy view. You can't achieve this effect with the z order because that affects only the nodes in Cocos2D's OpenGL view.
To make the Cocos2D view transparent you need to change the color depth to 32 bit (RGBA8888) from the default 16-Bit mode in the EAGLView initialization (app delegate). Then setup the view hierarchy with a dummy view, so that you can actually add UIKit views in the background and the Cocos2D view as the foremost view.
The basic approach is to do this in your app delegate didFinishLaunching method:
UIView* dummyView = [[UIView alloc] initWithFrame:[window bounds]];
[dummyView autorelease];
[dummyView addSubview:[CCDirector sharedDirector].openGLView];
rootViewController.view = dummyView;
[window addSubview:rootViewController.view];
// make the cocos2d view transparent:
glClearColor(0.0, 0.0, 0.0, 0.0);
[[CCDirector sharedDirector] openGLView].opaque = NO;
From then on you can use the dummyView to manage the view hierarchy. For example, when you add your game over view to the dummyView, you want to call sendToBack on it so that it is drawn behind the cocos2d view.
The entire process with all things to consider is described in detail in my Learn Cocos2D Game Development book (2nd Edition).

UIScrollView not calling layoutSubviews while zooming on iOS 4.0

I've read many of the UIScrollView questions asked on here but none of them answered this problem.
I have a UIScrollView that zooms a view containing other subviews. When a zoom is happening, I need to have the subviews of the view move to certain places. I can accomplish this by changing their frames accordingly in the layoutSubviews method of the UIScrollView.
On iOS 3.2 and lower, this works great both for finger-zooming and pragmatic zooming using zoomToRect:animated: method.
On iOS 4.0 or higher, this only works great for finger-zooming. With pragmatic zooming using zoomToRect:animated:, a call to layoutSubviews only happens once (at the end of the zoom). I need this method to get called many times during the duration of the zoom so that my subviews can have smooth position changes along with the zoom.
So in summary, on iOS 3.2, a call to layoutSubviews happens many times and on iOS 4.0 a call to layoutSubviews happens only once when you call zoomToRect:animated:. Does anyone know of a solution to my problem?
In IOS 4.0, animations are done by the OS - I assume to make use of GPU based hardware acceleration as much as possible. As a disadvantage of that, it becomes harder to animate a values that is derived from another (animated) value. As in your case, the positions of the subviews that depend on the zoom level of the UIScrollView.
In order to make that happen, you should setup the animation of the subviews to go in parallel with the animation of the zooming. Try something like:
[UIView animateWithDuration:0.5 delay:0
options:UIViewAnimationOptionLayoutSubviews
animations:^{
theScrollView.zoomScale = zoomScale;
[theScrollView layoutSubViews];
} completion:^(BOOL finished) {}];
This should set the frame properties of the subviews from within the same animation context, and therefore, they should be animated together by the OS.
I am not sure whether the explicit layoutSubViews is needed, or whether the UIViewAnimationOptionLayoutSubviews option already achieves that.
See also the answers to this question: How to make UIScrollView send scrollViewDidScroll messages during animations

emulate bounce effect in iphone app without UIScrollView

I am using a pan gesture to drag an item in my app. I know I could use a UIScrollView to get that "bounce" effect when dragging to an edge. However, that seems like a hack, so I'd like to find a way to get that effect when manually dragging an item (setting its frame or center point). I'm curious if there's a standard way, or a physics library in the Accelerate framework, something like that which wouldn't require me implementing my own physics engine.
The standard UIView animation curve is one that uses the accelerate / decelerate curve, which should gives a nice "bounce back" action.
It's really easy to use. Once you decide you want to "snap back" to a location, do this:
[UIView beginAnimations:nil context:nil];
CGRect newFrame = viewToAnimate.frame;
newFrame.origin.x = snapBackXcoordinate;
viewToAnimate.frame = newFrame;
[UIView commitAnimations];
to entirely emulate the UIScrollView you'll also need to set a "maximum stretch" distance before you stop stretching more.
But why re-invent the wheel? The UIKit components you're given are there to use. If putting your view into a UIScrollView does the trick, go ahead and use it. Saves you develop and debug time.