Analysis on various views of Cocos2d

Source: Internet
Author: User

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.

 
 
  1. [[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 ).

 
 
  1. CGFloat radian = 0;
  2. CGRect bounds; switch ([UIApplication sharedApplication]. statusBarOrientation)
  3. {
  4. Case UIInterfaceOrientationPortrait: break;
  5. Case UIInterfaceOrientationPortraitUpsideDown: radian = PI;
  6. Break; case UIInterfaceOrientationLandscapeLeft: radian =-PI/2;
  7. Bounds. size = CGSizeMake (480,320 );
  8. Break;
  9. Case UIInterfaceOrientationLandscapeRight:
  10. Radian = PI/2;
  11. Bounds. size = CGSizeMake (480,320 );
  12. Break;
  13. Default: break;
  14. }
  15. // Set the transform variable, centered on the center of the current view
  16. // CGAffineTransformMakeRotation can generate a transform matrix for rotating the current coordinate system,
  17. // The rotation angle is counter-clockwise radion, where radion is the radian value of this angle.
  18. CGAffineTransform transform = CGAffineTransformMakeRotation (radian );
  19. _ ContentView. transform = transform; // reset the view size.
  20. _ 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,

 
 
  1. [[CCDirector shareddire] detach]; // reset the orientation of the device.
  2. [[CCDirector sharedDirector] setDeviceOrientation: CCDeviceOrientationLandscapeLeft];
  3. // Re-attach to view [[CCDirector shareddire] attachInView: NSWindow];
  4. // Relay the current scene
  5. // 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,

 
 
  1. [[CCDirector shareddire] detach]; // attach to view again
  2. [[CCDirector shareddire] attachInView: NSWindow]; // relay the current scene
  3. // 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.