Stanford iOS Development Lesson Five (Part One)

Source: Internet
Author: User

Reprint please indicate the source

http://blog.csdn.net/pony_maggie/article/details/27706991


Author: Pony


Because the content of lesson five is more, it is divided into two parts to write.

Basic operation of a screen rotation


Controls whether the current view supports rotation, if yes, which direction is supported, there are four directions, the home key in the next, on, around.

In the current Viewcontroller, implement the Shouldautorotatetointerfaceorientation method, telling the system the direction of rotation you support, as follows:

-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation {   return YES;// //return uiinterfaceorientationisportrait (interfaceorientation) is supported in four directions,   and//supports only upright   //return ( Interfaceorientation==uiinterfaceorientationlandscapeleft)//transverse home key in left   //return (interfaceorientation== uiinterfaceorientationlandscaperight)//transverse home key at right   //return (interfaceorientation== uiinterfaceorientationportrait)//Vertical normal   //return (interfaceorientation==uiinterfaceorientationlandscapeleft)/ /Vertical Home Key on}


If this method is not implemented, the default is not to support rotation, only portrait.


However, after IOS6, this method is listed as deprecated method, can not be used again. Replace it with two new methods.

( Note that for backwards compatibility with IOS 4 and 5, you still need to keep it in your app Shouldautorotatetointerfaceorientation)

supportedinterfaceorientations and the shouldautorotate

If you cannot rotate, you only need to

-(BOOL) shouldautorotate{   


For example, to support horizontal

-(Nsuinteger) supportedinterfaceorientations{   return uiinterfaceorientationmaskallbutupsidedown;} -(BOOL) shouldautorotate{   return YES;

 

At the same time, it is necessary to Info.plist add the direction that the program supports in the file. If you do not implement both of these methods,theiphone only supports horizontal rotation by default.

two Struts and Springs

This is an old layout model, xcode4.5 after the default is to use AutoLayout, so you will not see the following Struts and springs, such as :


If you want to use this, in the project Use AutoLayout box is checked out. Struts and Springs can set when the parent view of a view changes in size, it needs to make changes to itself. For example, when you change from portrait to landscape, it may be necessary to do some stretching for some controls. Notice the left side part, divided into two parts, the outer layer is something like the capital letter I , these controls when the parent view changes, the child view should be how to " rely on ", like an arrow in the inner layer controls how the child view should "stretch" when the parent view changes. Of course Struts and springs are not omnipotent, it can only handle some simple situations. There are some scenarios that need to be done in code. Imagine that if a calculator's application, from portrait to landscape, can not simply stretch a few buttons on the line, this situation will write code to do some layout.

as space is limited, here is not a detailed example, recommend an article on the automatic layout is better :

Http://www.cocoachina.com/applenews/devnews/2013/1203/7462.html

Three-protocol

This class has about 10 minutes to talk about the agreement, because my previous article on this has been described, here do not do too much introduction.

Four gesture recognition

The heart of gesture recognition is the Uigesturerecognizer class, with two steps to add a gesture recognition function, one is to join the gesture in UIView, and the other is to implement a processing function, which can be seen in the next section of the sample code.

It is important to note that the first step is usually done in the controller, and the second is done in UIView. This thought stems from the reusability of the UIView, that is, we have to consider that a uiview will be used by multiple controllers, and the controller that may not be needed will have different gesture recognition requirements.


Here is an example of adding gesture recognition, which is of course best placed in the Controller implementation section. The pan function in this case is the processing function mentioned above, which is the action to be performed when a gesture is triggered.


The above gives the methods and properties provided by Uigesturerecognizer (or its subclasses), which assist us in implementing the handler function because a default parameter of the handler is the actual Uigesturerecognizer (or its subclasses). For example, this code:

Gesture recognition, zoom function-(void) Pinch: (Uipinchgesturerecognizer *) gesture{    if ((gesture.state = = uigesturerecognizerstatechanged) | |        (gesture.state = = uigesturerecognizerstateended))    {        /* The         following two lines of code are actually the same as this one, so be aware of the effect of the second line 1. You can check how Gesture.scale is         self.scale = Gesture.scale;         */        Self.scale *= Gesture.scale;        NSLog (@ "scale:%f", Gesture.scale);        Gesture.scale = 1;    }}



Then we see that there are three specific sub-classes of Uigesturerecognizer, which are used to handle zooming, flipping, and swiping gestures.

In the second part we'll talk about code examples.

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.