Time-consuming operations for creating a UI in iOS development and time-consuming ios development ui
When there are a series of time-consuming operations such as network requests and read/write operations in the project, in order to avoid blocking the main thread, we will put these time-consuming operations in the Child thread for processing. After the processing is complete, return to the main thread to update the UI, so that the main thread will not be blocked. However, when creating a UI, it is generally executed in the main thread. If there are many UI controls to be created, unfriendly freezing may occur, resulting in poor experience, for example, when pushed to a ViewController, a large number of view and view sub-classes are created in the ViewController due to project requirements. During page Jump, the page may experience unfriendly freezing.
In this case, the simple method is to use it directly.
[self performSelector:(nonnull SEL) withObject:(nullable id) afterDelay:(NSTimeInterval) inModes:(nonnull NSArray<NSString *> *)];
This method has four parameters. The first parameter can be understood as the name of the method to be called. The second parameter indicates the parameter carried by the method to be called (if there is no parameter, pass nil ); the third parameter indicates the number of seconds of execution delay (if you do not need to delay the execution, you can pass 0.0). The last parameter is an array, and the elements in the array are RunLoop mode (NSDefaultRunLoopMode and nsunloopcommonmodes ).
-(Void) viewDidLoad {[super viewDidLoad]; [self defined mselector: @ selector (setupUI) withObject: nil afterDelay: 0.0f inModes: @ [nsunloopcommonmodes];} // This code is executed using the given Runloop mode in the current thread, not necessarily in the main thread. If you want to ensure that the task is executed in the main thread, you can use // your mselecw.mainthread: withObject: waitUntilDone: Or // your mselecw.mainthread: withObject: waitUntilDone: modes: instead of-(void) setupUI {// The UI operation that blocks the main thread can be put here for (int I = 0; I <10000; I ++) {UIView * view = [[UIView alloc] initWithFrame: CGRectMake (0, I * 2 + 10, 5, 5)]; view. backgroundColor = [UIColor redColor]; [self. view addSubview: view]; NSLog (@ "time-consuming UI operation"); NSLog (@ "% @", view );}}