Imitation Sina Weibo iOS client four-no login page processing

Source: Internet
Author: User

Written in the front: I am very sorry, for personal reasons for a more than 20-day period did not continue to update the topic, the period is only completed the official Apple Uistackview document translation. Here we will continue the unfinished task, continue to do! Now go on!

First, the preliminary summary

After completing the third task, we actually completed the following results:

  

We are now able to ensure the smooth switching of the interface, and to the micro-blogging button to reserve the Click event interface, and then the next step, we will be completed before the user login all the preparation work.

  

Ii. extraction of base classes

At present, we in the home page, the message, the discovery, I these four interfaces use is uitableviewcontroller to load and the display content, although in the actual display the difference, but in the nature we can think the same, therefore these four we can draw out a common base class, For four tableviewcontroller to inherit, to achieve the function we want.

1. Create a class that inherits from Uitableviewcontroller: BASETVC;

2. Modify four pages face the parent class of the Controller class;

3. Write the login judgment code in BASETVC and test.

The code written in BASETVC is as follows:

    // User Login Tag    false        Override func Loadview () {        //  User is logged in, execution by default        if  userlogon {            Super.loadview ()            return        }        //  user not logged in, modify Controller view to blank view         view = UIView ()    }

In the case where the user is logged in, the effect is the same as not writing this code, so we only test the effect that is not logged in, that is, Userlogon = False. The test results are as follows:

  

Here we can see, in the case of the user is not logged in, did create a blank view instead of the original TableView, and this blank view will be our implementation of the user login view effect location.

  

Third, user login view

Weibo login interface is basically as shown, the middle is a page related to the large icon, the interface changes when the large icon will switch, a small circle of icons around the large icon rotation, and the view has a transparency gradient effect, large icons at the bottom of the small icon is a looming effect, and other content and the picture shows the basic consistent.

1. First drag into the material

2. Create Xib

Since our view is created using code, we are not able to operate on storyboard, and if we want to handle it, we have only two options: pure code operation/xib Loading. Pure code development is feasible, but given the efficiency advantages of visual operations, we are using xib here.

To create the Xib file Visitorloginview, drag into the control and set the layout as follows:

  

Drag in order: Small icon picture, mask picture, large icon picture->label-> registration button, login button

Constraint order:

The Mask is aligned to the left, right, and top three sides of the xib, with the spacing below 110;

Large icon Center, width and height are 90;

Small icon Reference Large icon center alignment, width and height are 180;

Label reference small icon horizontal center alignment, Vertical spacing 8, Width 224, height 34, font size 14, line number 2

Button reference label left and right aligned, vertical spacing 15, Width 106, height 34, font size 18, the text color is orange and black respectively

Note: The default drag-in order reflects the occlusion between the individual pictures, and the more the control that is dragged into it, the more it blocks the picture that was dragged in before it, so in this order you can ensure that the mask is obscured by the small icon, and that the mask image is a gradient effect, so you can display a small icon image of the gradient.

Also, constraints on all controls except masks refer to large icons, so if a large icon moves, all controls maintain the current relative position of the same move, which is a convenient way to develop.

After completing the above steps, modify the following code, test again, test results such as:

        //         view = UIView ()        view = Nsbundle.mainbundle (). loadnibnamed ("visitorloginview " as! UIView    

The part of the UI setup is basically declared complete, and we just need to toggle the icon and text content based on the different pages.

3. Create a custom view class to add code to the Xib file

Create the Visitorloginview.swift file and write the following code:

@IBOutlet weak var smallicon:uiimageview!@IBOutlet weak var bigicon:uiimageview!@IBOutlet weak var msglabel:uilabel!//Click the Register button event method@IBAction func visitorregister (Sender:uibutton) {println (__function__)}//Click the Login button event method@IBAction func visitorlogin (Sender:uibutton) {println (__function__)}/** Set Guest interface information:p aram:iconname Display icon name:p aram:message Information label content:p Aram:ishome Home*/func setupvisitorinfo (iconname:string, message:string, Ishome:bool=false) {        //Non-home case: Hide large icons, display pictures to small icons//Home Case: Large icon display, and render default pictureBigicon.hidden =!IshomeifIshome {smallicon.image= UIImage (named:"Visitordiscover_feed_image_smallicon") Bigicon.image=UIImage (named:iconname)}Else{smallicon.image=UIImage (named:iconname)} Msglabel.text=message}

In this class, we have reserved an interface method, Setupvisitorinfo, to set the corresponding picture and label contents.

Once this is done, we need to call this interface in each controller to assign values to the view displayed under different interfaces.

This shows the benefit of extracting the base class: We only need to give a property to the base class, so all classes that inherit the base class will automatically get the properties owned by the base class, so here we add a property:

// Login View Properties    var visitorloginview:visitorloginview?

The Loadview method of the Controller base class is then partially modified again:

    // The user is not logged in and modifies the view that the controller view loads for the specified xib    Visitorloginview = Nsbundle.mainbundle (). loadnibnamed ("visitorloginview"as?  Visitorloginview    = Visitorloginview

Finally, add the following code to the Viewdidload method of the four sub-controllers:

Homepagetableviewcontroller

    Visitorloginview?. Setupvisitorinfo ("visitordiscover_feed_image_house" " attention to some people, Come back here and see what surprises "true"

Messagetableviewcontroller

    Visitorloginview?. Setupvisitorinfo ("visitordiscover_image_message"" login, people comment on your Weibo, send you the message, will receive a notice here ")

Discovertableviewcontroller

    Visitorloginview?. Setupvisitorinfo ("visitordiscover_image_message"" login, the latest, hottest microblogging in the grasp, will no longer pass with the tide of reality ")

Profiletableviewcontroller

    Visitorloginview?. Setupvisitorinfo ("visitordiscover_image_profile"" login, your Weibo, album, The profile will be displayed here and shown to others ")

Note: A feature of Swift syntax is used here: This parameter can be omitted if a parameter of a method is given a default value, and omitting this parameter means that the default value will be passed in. Here, our three-time invocation interface omits the Ishome parameter.

After completing the above setup, run the test again and we see the following test results:

Next, we will realize the small icon rotation effect on the homepage.

4. Homepage animation effect

In the original micro-blog, the small icon around the large icon image is constantly rotating, we can also make such a effect here. The steps are as follows:

To create an animation method in a custom Xib class:

 //  start animation  private   Func startanimation () {let anim  = Cabasicanimation (keypath: Span style= "color: #800000;" > " transform.rotation  "  ) anim.repeatcount  = Maxfloat anim.duration  = 20   Anim.tovalue  = m_pi * 2   SmallIcon.layer.addAnimation (Anim, Forkey:   smalliconrotationanim   )}  

Rewrite setupvisitorinfo:

Func setupvisitorinfo (iconname:string, message:string, Ishome:bool =false) {        //Non-home case: Hide large icons, display pictures to small icons//Home Case: Large icon display, and render default pictureBigicon.hidden =!IshomeifIshome {bigicon.image=UIImage (named:iconname)if(SmallIcon.layer.animationForKey ("Smalliconrotationanim") ==Nil) {startanimation ()}} Else{smallicon.image=UIImage (named:iconname)} Msglabel.text=message}

Here are the points to note:

Anim.removedoncompletion default to True in core animations
When the view disappears, the animation is automatically removed from the layer, so you don't need to stop the animation method
When adding animations, if you specify a key, the layer will be strongly referenced to the animation and will not be released, preventing the animation from stopping after the interface switch and no more booting.

Therefore, in the above code, you need to set the key to the animation added, to avoid the interface switch animation is released.

At the same time, because the Viewdidload method of a controller is only called when the view is loaded, this method is not called when the interface is switched. For the above reasons, the method of setting picture type and text in Homepagetableviewcontroller Setupvisitorinfo adjusted to Viewwillappear method.

    Override func viewdidload () {        super.viewdidload ()    }        override  func viewwillappear (animated : Bool) {        super.viewwillappear (animated)        Visitorloginview?. Setupvisitorinfo ("visitordiscover_feed_image_house" " attention to some people, Come back here and see what the surprise is . "true)    }

After completing the above steps, the following results are achieved:

  

At this point, the homepage animation effect is complete.

5. Registration, Login button click Agent

The login interface is also to deal with the registration and login button click. Here we use the Proxy method to handle the click of the event.

The first thing to note is that the use of proxies is divided into two parts: proxy declarations and the implementation of proxies

The declaration of an agent is divided into three parts: Define proxy properties, define proxy protocols and methods, and invoke proxy methods at the appropriate time;

The implementation of the proxy also has three parts: the registration Agent object, the implementation of Agent Protocol, and implement the Proxy method.

5.1 Proxy Statement

Not much to say, on the code

/** * Define proxy protocols and methods*/protocol Visitorloginviewdelegate:nsobjectprotocol {func didregisterbuttonclicked () func didloginbuttonclicked ()}classVisitorloginview:uiview {//Defining Proxy PropertiesWeak VarDelegate: Visitorloginviewdelegate?//Click the Register button event method@IBAction func visitorregister (Sender:uibutton) {//invoke the Proxy method at the right time        Delegate?. didregisterbuttonclicked ()}//Click the Login button event method@IBAction func visitorlogin (Sender:uibutton) {//invoke the Proxy method at the right time        Delegate?. didloginbuttonclicked ()}

Here, we first define the proxy protocol and method, according to the proxy attribute type defines the Proxy property, because the proxy method is called when the button is clicked, so the calling proxy method is also called in the button click event Method.

5.2 Proxy implementations

Registering a proxy object in BASETVC

    // registering proxy objects    Visitorloginview?. delegate = self

Complying with proxy protocols and implementing proxy methods

Extension basetvc:visitorloginviewdelegate {    func didregisterbuttonclicked () {        println (__function__);    }        func didloginbuttonclicked () {        println (__function__);    }}

Add buttons on both sides of the navigation bar and register for click events:

    " Register " " didregisterbuttonclicked " )    " login ""didloginbuttonclicked")

After completing the above code, the test results are as follows:

6. Set the navigation bar global appearance

Our code manually added button is the system default style, is the blue button, its style and the entire application of orange, gray color matching does not coordinate, in addition to other pages may also need to set the appearance, so we can set the global appearance, to determine the appropriate style at once.

class Appdelegate:uiresponder, uiapplicationdelegate {    var window:uiwindow?     Func application (application:uiapplication, didfinishlaunchingwithoptions launchoptions: [Nsobject:anyobject] ?) -Bool {                setupappearance ()                returntrue    }        //  set global skin style    private  func setupappearance () {        =  Uicolor.orangecolor ()    }

When the above settings are complete, the navigation bar buttons become orange with app tones.

At this point, all the preparation is done before the user can log on.

Zatan

1. Interface

In the development process, we tend to reserve the interface for some untapped functions and code, which is a very necessary means and method, it can also be understood as a todo matter, we need to do it. From the beginning of this project to the present, we have left a lot of interfaces (or "pits") in our project, and then we will fill them in one after the other.

Agents in 2.Swift

The agent in Swift is basically the same as OC, and the idea of implementation can be said to be exactly the same, but there are some details that need to be highlighted.

When defining the protocol, you need to inherit from: Nsobjectprotocol. This is a big difference from OC.

When defining proxy properties, we use the weak field to decorate the properties to avoid circular references. If it is the default strong, the controller will strongly refer to view, which will strongly reference the view's proxy when registering the Proxy property, and if view strongly references its own proxy, a circular reference will be present.

Imitation Sina Weibo iOS client four-no login page processing

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.