iOS development time-consuming handling of creating UI

Source: Internet
Author: User

In the event of a series of time-consuming operations such as network requests, read-write operations, and so on, in order to avoid blocking the main thread, we put these time-consuming operations into the sub-threads, and when the processing is complete, go back to the main thread to update the UI so that the main thread is not blocked. However, when creating the UI is usually done in the main thread, if you need to create more UI controls, there may be a very unfriendly lag, experience is poor, such as push to a certain viewcontroller, due to project requirements, The Viewcontroller created a more view and view sub-class, the page in the jump, there will be a very unfriendly lag phenomenon.
The simple way to do this is to use it directly.

ID) Afterdelay: (Nstimeinterval) Inmodes: (nonnull nsarray<nsstring *> *)];

This method has four parameters, the first parameter can be interpreted as the name of the method to invoke, and the second parameter indicates the parameters to be called by the method (if no argument, nil can be passed); The third parameter indicates how many seconds delay is performed (if you do not delay the execution of 0.0); The last argument is an array. The elements in the array are Runloop mode (Nsdefaultrunloopmode and Nsrunloopcommonmodes).

- (void) viewdidload {[Super viewdidload]; [Self performselector: @selector (SETUPUI) withobject:nil Afterdelay:0.0fInmodes:@[nsrunloopcommonmodes]];}//This code is executed in the current thread using the given specific runloop mode, and not necessarily in the main threads. If you want to ensure execution in the main thread, you can use the//PerformSelectorOnMainThread:withObject:waitUntilDone: or//performSelectorOnMainThread:withObject:waitUntilDone:modes: To replace- (void) Setupui {//the UI action that blocks the main thread can be placed here to execute     for(intI=0; i<10000; i++) {UIView*view = [[UIView alloc] Initwithframe:cgrectmake (0, i*2+Ten,5,5)]; View.backgroundcolor=[Uicolor Redcolor];        [Self.view Addsubview:view]; NSLog (@"time consuming UI actions"); NSLog (@"%@", view); }}

Transferred from: http://www.jianshu.com/p/82c98bf6ef81

iOS development time-consuming handling of creating UI

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.