Cocos2dVariousViewConversion analysis in this article, aboutCocos2dYou are familiar with the following screen rotation:Cocos2dIf you want to set it to landscape, you only need to call the following function before attachView.
- [[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeLeft];
However, in actual development, we often have the following problems:Cocos2dAdd UIViewCocos2dAttach is notNSWindowHowever, when other subviews encounter these problems, you will find things are not as simple as you expected. To solve these problems, we must first understand how iPhone screen rotation is handled.
First, each View on the iPhone represents a layer, and each layer has itsCoordinatesSystem. Take the iPhone as an example.CoordinatesIt is a system with a height of 480 and a width of 320. In the landscape, if you have processed the screen rotationCoordinatesThe system should be 320 in height and 480 in width. When you use UIViewController, the results are obvious. However, when you create a UIView and add itNSWindowYou will find that this view is still in the portrait status, regardless of the portrait screen.
If you get the NSWindow or your own View bounds, you will get 320x00002 instead of 480x320. What is the problem? Think about it and we will understand it. UIViewController provides a method for us to tell whether to choose its managed UIView, while neither NSWindow nor UIApplication provides similar methods. That is to say, the screen Rotation Processing requires the View Controller to listen to the device rotation event from the row for processing, and the UIViewController provides a similar implementation, this allows you to rotate the view with the screen without any operations.
If you create a View on your own without attaching it to any ViewController, You need to monitor the screen rotation event on your own. How can we rotate a View? Each UIView has a transform attribute, which determines the Coordinate Transformation of the default Coordinate System of the View. Therefore, transform is actually a three-dimensional matrix, it is used to convert the coordinates of a point in the two coordinate systems. opengl is a three-dimensional system, so it is a three-dimensional coordinate ). The default coordinate system uses the center of the View as the origin, the width of the View is the horizontal axis, and the height is the vertical axis.
Of course, this system is based on its superView. If the coordinate system of the superView is the portrait coordinate, the default Coordinate System of the view is the portrait coordinate. If the coordinate system of the superView is the landscape coordinate, the default Coordinate System of the view is the landscape coordinate. The following describes how to change the orientation of a View by setting transform Based on the coordinate coordinates of a subview in NSWindow, the reason is that NSWindow does not do transform in the system. If you use the subview of a UIViewController to do transform, the following code is inaccurate ).
- CGFloat radian = 0;
- CGRect bounds; switch ([UIApplication sharedApplication]. statusBarOrientation)
- {
- Case UIInterfaceOrientationPortrait: break;
- Case UIInterfaceOrientationPortraitUpsideDown: radian = PI;
- Break; case UIInterfaceOrientationLandscapeLeft: radian =-PI/2;
- Bounds. size = CGSizeMake (480,320 );
- Break;
- Case UIInterfaceOrientationLandscapeRight:
- Radian = PI/2;
- Bounds. size = CGSizeMake (480,320 );
- Break;
- Default: break;
- }
- // Set the transform variable, centered on the center of the current view
- // CGAffineTransformMakeRotation can generate a transform matrix for rotating the current coordinate system,
- // The rotation angle is counter-clockwise radion, where radion is the radian value of this angle.
- CGAffineTransform transform = CGAffineTransformMakeRotation (radian );
- _ ContentView. transform = transform; // reset the view size.
- _ ContentView. bounds = bounds;
Now let's look back at the aforementionedCocos2d.
1. The default cocos2d program automatically rotates cocos2d openglview attache to NSWindow. In this case, you need to listen to the UIDeviceOrientationDidChangeNotification event and reconfigure cocos2d when the device rotates. (Do not listen to the corresponding delegate and setDeviceOrientation: CCDeviceOrientationLandscapeLeft events of the UIApplicationDidChangeStatusBarOrientationNotification or UIApplication to trigger these events ).
In the listener processing function, you generally need to do the following,
- [[CCDirector shareddire] detach]; // reset the orientation of the device.
- [[CCDirector sharedDirector] setDeviceOrientation: CCDeviceOrientationLandscapeLeft];
- // Re-attach to view [[CCDirector shareddire] attachInView: NSWindow];
- // Relay the current scene
- // There is no ready-made method here. You can implement it by yourself.
If your program attaches cocos2d openglview to a ViewController view, the situation will be different, because ViewController supports automatic screen rotation. If you want ViewController to handle screen rotation, for example, you still need to display the navigation bar), you cannot call the [[CCDirector shareddire] setDeviceOrientation:...] method.
You can think about the reason. The only thing you need to do is to relay the scene during screen rotation. So the code is simplified to this,
- [[CCDirector shareddire] detach]; // attach to view again
- [[CCDirector shareddire] attachInView: NSWindow]; // relay the current scene
- // There is no ready-made method here. You can implement it by yourself.
2.Cocos2dIn this caseNSWindowIt is similar to adding a UIView. If you create a view, you need to set the viewCoordinatesConversion. If your view is managed by viewcontroller, you do not need to handle it yourself.
3,Cocos2dAttach is notNSWindowOther Subview cases have already been mentioned in section 1. If the View you want to attach is managed by UIViewController and you allow the Controller to rotate the screen, you cannot call [[CCDirector shareddire] setDeviceOrientation:...] method, otherwise you will findViewIt is rotated once more.
Summary: AboutCocos2dVariousViewThe Analysis of conversion is complete. I hope this article will help you!