Read Source code series-mbprogresshud (load wait box)

Source: Internet
Author: User

Before reading this article, download the source code (portal)

Mbprogresshud is an iOS class. When a background process is executed, a translucent HUD containing an indicator or tag is displayed. HUD is a replacement of uiprogresshud which is not allowed by developers in the uikit package.

Prerequisites

Mbprogresshud can run in any version of IOS in arc or non-arc mode. It uses the following cocoa touch framework

  • Foundation. Framework
  • Uikit. Framework
  • CoreGraphics. Framework

From mbprogresshud. h. First, mbprogresshud inherits the sub-uiview, which has the view Feature. In its header file, the enumerated types mbprogresshudmode and mbprogresshudanimation correspond to different loading wait box styles and disappear animation types respectively. The following macro definition marks the arc and non-arc modes and supports block. Through the class method +
(Mbprogresshud *) showhudaddedto :( uiview *) view animated :( bool) animated;
You can also use the object method alloc and initwithview to obtain the mbprogresshud object. You can set the object that complies with the mbprogresshuddelegate protocol as a delegate and callback when mbprogresshud is about to be hidden.

When the program encounters an asynchronous or time-consuming task that needs to be executed, you can use-(void) showwhileexecuting :( SEL) method ontarget :( ID) Target withobject :( ID) object animated :( bool) animated; method, "Lock" the main interface and display the loading wait box. The focus is on the function. Let's take a look:

- (void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object animated:(BOOL)animated {methodForExecution = method;targetForExecution =MB_RETAIN(target);objectForExecution =MB_RETAIN(object);// Launch execution in new threadself.taskInProgress =YES;[NSThreaddetachNewThreadSelector:@selector(launchExecution)toTarget:selfwithObject:nil];// Show HUD view[selfshow:animated];}

First, after the tasks and target objects to be executed are passed in, mb_retain is actually a macro. In the header file mentioned earlier, under non-arc, it will first add the memory count of the target and object to 1, and be familiar with memory management. After receiving the parameter, it should first retain to prevent objects from being released in advance. Next we will use nsthread to open a thread. This is the focus. Now we have a new thread. It will execute our passed tasks, but it will not be called in the main thread, in this way, the main thread will not be blocked. The new thread sends a message to launchexecution.

- (void)launchExecution {@autoreleasepool {#pragma clang diagnostic push#pragma clang diagnostic ignored "-Warc-performSelector-leaks"// Start executing the requested task[targetForExecutionperformSelector:methodForExecutionwithObject:objectForExecution];#pragma clang diagnostic pop// Task completed, update view in main thread (note: view operations should// be done only in the main thread)[selfperformSelectorOnMainThread:@selector(cleanUp)withObject:nilwaitUntilDone:NO];}}

Targetforexecution completes the execution of a specific task. At this time, this thread is always executing our task. mongomselecw.mainthread means to execute cleanup in the main thread, so when our task is executed in other threads, execute cleanup when returning to the main thread.

After cleanup executes some release and nil assignment operations, it will execute-(void) Hide :( bool) animated, which will execute the method that triggers the animation effect, and then call-
(Void) done, which contains such code

if ([delegaterespondsToSelector:@selector(hudWasHidden:)]) {[delegateperformSelector:@selector(hudWasHidden:)withObject:self];}

Send the hudwashidden message to the proxy object. If this is set before, you can respond.

In the header file

@property (atomic, copy) NSString *labelText;@property (atomic, copy) NSString *detailsLabelText;

If we assign values to these two nsstring attributes, the label in the interface will be directly updated. Here we use KVO programming. We can view this part of the Code through the # pragma mark-KVO mark, all key value information

- (NSArray *)observableKeypaths {return [NSArray arrayWithObjects:@"mode", @"customView", @"labelText", @"labelFont", @"detailsLabelText", @"detailsLabelFont", @"progress", nil];}

Register a key value

- (void)registerForKVO {for (NSString *keyPath in [self observableKeypaths]) {[self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:NULL];}}

Update the UI when the key value changes.

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {if (![NSThread isMainThread]) {[self performSelectorOnMainThread:@selector(updateUIForKeypath:) withObject:keyPath waitUntilDone:NO];} else {[self updateUIForKeypath:keyPath];}}

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.