├── src ├── ios │ ├── SpinnerPlugin.h │ ├── SpinnerPlugin.m │ ├── MBProgressHUD.h │ └── MBProgressHUD.m └── android │ ├── spinner_plugin_styles.xml │ ├── FakeR.java │ └── SpinnerPlugin.java ├── typings ├── cordova-plugin-spinner-tests.ts └── cordova-plugin-spinner.d.ts ├── package.json ├── LICENSE ├── www └── spinner-plugin.js ├── plugin.xml └── README.md /src/ios/SpinnerPlugin.h: -------------------------------------------------------------------------------- 1 | // 2 | // SpinnerPlugin.h 3 | // 4 | // Copyright (c) 2015 Justin Unterreiner. All rights reserved. 5 | // 6 | 7 | #import 8 | #import 9 | #import "MBProgressHUD.h" 10 | 11 | @interface SpinnerPlugin : CDVPlugin 12 | - (void)activityStart:(CDVInvokedUrlCommand *)command; 13 | - (void)activityStop:(CDVInvokedUrlCommand *)command; 14 | @end -------------------------------------------------------------------------------- /typings/cordova-plugin-spinner-tests.ts: -------------------------------------------------------------------------------- 1 | /// 2 | 3 | let options: SpinnerPlugin.Options = { 4 | dimBackground: true 5 | }; 6 | 7 | SpinnerPlugin.activityStart(); 8 | SpinnerPlugin.activityStart("a"); 9 | SpinnerPlugin.activityStart("a", null); 10 | SpinnerPlugin.activityStart("a", options); 11 | SpinnerPlugin.activityStart("a", options, () => {}); 12 | SpinnerPlugin.activityStart("a", options, () => {}, () => {}); 13 | 14 | SpinnerPlugin.activityStop(); 15 | SpinnerPlugin.activityStop(() => {}); 16 | SpinnerPlugin.activityStop(() => {}, () => {}); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cordova-plugin-spinner", 3 | "version": "1.1.0", 4 | "description": "A Cordova plugin to prevent user interaction using an animated spinner overlay during a blocking operation.", 5 | "cordova": { 6 | "id": "cordova-plugin-spinner", 7 | "platforms": [ 8 | "android", 9 | "ios" 10 | ] 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/justin-credible/cordova-plugin-spinner" 15 | }, 16 | "keywords": [ 17 | "cordova", 18 | "spinner", 19 | "progress", 20 | "activity", 21 | "block", 22 | "ecosystem:cordova", 23 | "cordova-android", 24 | "cordova-ios" 25 | ], 26 | "license": "MIT" 27 | } 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Justin Unterreiner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /src/android/spinner_plugin_styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 12 | 24 | #00000000 25 | -------------------------------------------------------------------------------- /www/spinner-plugin.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var exec = require("cordova/exec"); 4 | 5 | /** 6 | * The Cordova plugin ID for this plugin. 7 | */ 8 | var PLUGIN_ID = "SpinnerPlugin"; 9 | 10 | /** 11 | * The plugin which will be exported and exposed in the global scope. 12 | */ 13 | var SpinnerPlugin = {}; 14 | 15 | /** 16 | * Blocks user input using an indeterminate spinner. 17 | * 18 | * An optional label can be shown below the spinner. 19 | * 20 | * @param [string] labelText - The optional value to show in a label. 21 | * @param [options] options - The optional options object used to customize behavior. 22 | * @param [function] successCallback - The success callback for this asynchronous function. 23 | * @param [function] failureCallback - The failure callback for this asynchronous function; receives an error string. 24 | */ 25 | SpinnerPlugin.activityStart = function activityStart(labelText, options, successCallback, failureCallback) { 26 | 27 | // Ensure that only string values are passed in. 28 | if (typeof(labelText) !== "string") { 29 | labelText = ""; 30 | } 31 | 32 | if (!options || typeof(options) !== "object") { 33 | options = {}; 34 | } 35 | 36 | if (typeof(options.dimBackground) !== "boolean") { 37 | options.dimBackground = true; 38 | } 39 | 40 | exec(successCallback, failureCallback, PLUGIN_ID, "activityStart", [labelText, options.dimBackground]); 41 | }; 42 | 43 | /** 44 | * Allows user input by hiding the indeterminate spinner. 45 | * 46 | * @param [function] successCallback - The success callback for this asynchronous function. 47 | * @param [function] failureCallback - The failure callback for this asynchronous function; receives an error string. 48 | */ 49 | SpinnerPlugin.activityStop = function activityStop(successCallback, failureCallback) { 50 | exec(successCallback, failureCallback, PLUGIN_ID, "activityStop", []); 51 | }; 52 | 53 | module.exports = SpinnerPlugin; 54 | -------------------------------------------------------------------------------- /plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | Spinner Plugin 8 | 9 | 10 | A Cordova plugin to prevent user interaction using an animated spinner overlay during a blocking operation. 11 | 12 | 13 | MIT 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /typings/cordova-plugin-spinner.d.ts: -------------------------------------------------------------------------------- 1 | // Type definitions for cordova-plugin-spinner 1.1.0 2 | // Project: https://github.com/Justin-Credible/cordova-plugin-spinner 3 | // Definitions by: Justin Unterreiner 4 | // Definitions: https://github.com/borisyankov/DefinitelyTyped 5 | 6 | declare module SpinnerPlugin { 7 | 8 | interface SpinnerPluginStatic { 9 | 10 | /** 11 | * Blocks user input using an indeterminate spinner. 12 | * 13 | * An optional label can be shown below the spinner. 14 | * 15 | * @param labelText The optional value to show in a label. 16 | * @param options - The optional options object used to customize behavior. 17 | * @param successCallback The success callback for this asynchronous function. 18 | * @param failureCallback The failure callback for this asynchronous function; receives an error string. 19 | */ 20 | activityStart(labelText?: string, options?: Options, successCallback?: () => void, failureCallback?: (error: string) => void): void; 21 | 22 | /** 23 | * Allows user input by hiding the indeterminate spinner. 24 | * 25 | * @param successCallback The success callback for this asynchronous function. 26 | * @param failureCallback The failure callback for this asynchronous function; receives an error string. 27 | */ 28 | activityStop(successCallback?: () => void, failureCallback?: (error: string) => void): void; 29 | } 30 | 31 | /** 32 | * The options object used to customize the dialog's behavior. 33 | */ 34 | interface Options { 35 | 36 | /** 37 | * True to use a dimmed background which overlays all content. 38 | * 39 | * If not provided, defaults to true. 40 | */ 41 | dimBackground?: boolean; 42 | } 43 | } 44 | 45 | declare var SpinnerPlugin: SpinnerPlugin.SpinnerPluginStatic; 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spinner Cordova Plugin 2 | 3 | This is a [Cordova](http://cordova.apache.org/) plugin for preventing user interaction using an animated spinner overlay during a blocking operation. 4 | 5 | On Android the native `ProgressDialog` is used, while on iOS it uses the [`MBProgressHud` library](https://github.com/jdg/MBProgressHUD). 6 | 7 | # Install 8 | 9 | To add the plugin to your Cordova project, simply add the plugin from the npm registry: 10 | 11 | cordova plugin add cordova-plugin-spinner 12 | 13 | Alternatively, you can install the latest version of the plugin directly from git: 14 | 15 | cordova plugin add https://github.com/Justin-Credible/cordova-plugin-spinner 16 | 17 | # Usage 18 | 19 | The plugin is available via a global variable named `SpinnerPlugin`. It exposes the following properties and functions. 20 | 21 | All functions accept optional success and failure callbacks as their final two arguments, where the failure callback will receive an error string as an argument unless otherwise noted. 22 | 23 | A TypeScript definition file for the JavaScript interface is available in the `typings` directory as well as on [DefinitelyTyped](https://github.com/borisyankov/DefinitelyTyped) via the `tsd` tool. 24 | 25 | # Indeterminate Spinner 26 | 27 | ## Show Spinner ## 28 | 29 | Blocks user input using an indeterminate spinner. 30 | 31 | Method Signature: 32 | 33 | `activityStart(labelText, options, successCallback, failureCallback)` 34 | 35 | Parameters: 36 | 37 | * `labelText` (string): The (optional) attribute text to use for the spinner label. 38 | * `options` (object): The (optional) options object used to customize behavior. 39 | * `dimBackground` (boolean): True to use a dimmed background which overlays all content. (defaults to true) 40 | 41 | Example Usage: 42 | 43 | ```` 44 | var options = { dimBackground: true }; 45 | SpinnerPlugin.activityStart("Loading...", options); 46 | ```` 47 | 48 | ## Hide Spinner ## 49 | 50 | Allows user input by hiding the indeterminate spinner. 51 | 52 | Method Signature: 53 | 54 | `activityStop(successCallback, failureCallback)` 55 | 56 | Example Usage: 57 | 58 | `SpinnerPlugin.activityStop();` 59 | -------------------------------------------------------------------------------- /src/android/FakeR.java: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License 3 | 4 | Copyright (c) 2010 Matt Kane 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 11 | 12 | Code taken from: https://github.com/jeduan/cordova-plugin-imagepicker/ 13 | */ 14 | package net.justin_credible.cordova; 15 | 16 | import android.app.Activity; 17 | import android.content.Context; 18 | 19 | /** 20 | * R replacement for PhoneGap Build. 21 | * 22 | * ([^.\w])R\.(\w+)\.(\w+) 23 | * $1fakeR("$2", "$3") 24 | * 25 | * @author Maciej Nux Jaros 26 | */ 27 | public class FakeR { 28 | private Context context; 29 | private String packageName; 30 | 31 | public FakeR(Activity activity) { 32 | context = activity.getApplicationContext(); 33 | packageName = context.getPackageName(); 34 | } 35 | 36 | public FakeR(Context context) { 37 | this.context = context; 38 | packageName = context.getPackageName(); 39 | } 40 | 41 | public int getId(String group, String key) { 42 | return context.getResources().getIdentifier(key, group, packageName); 43 | } 44 | 45 | public static int getId(Context context, String group, String key) { 46 | return context.getResources().getIdentifier(key, group, context.getPackageName()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/ios/SpinnerPlugin.m: -------------------------------------------------------------------------------- 1 | // 2 | // SpinnerPlugin.m 3 | // 4 | // Copyright (c) 2015 Justin Unterreiner. All rights reserved. 5 | // 6 | 7 | #import "SpinnerPlugin.h" 8 | #import 9 | 10 | @interface SpinnerPlugin() 11 | 12 | @end 13 | 14 | @implementation SpinnerPlugin 15 | 16 | MBProgressHUD *progressIndicator; 17 | 18 | #pragma mark - Cordova commands 19 | 20 | - (void)activityStart:(CDVInvokedUrlCommand *)command { 21 | 22 | // Ensure we have the correct number of arguments. 23 | if ([command.arguments count] != 2) { 24 | CDVPluginResult *res = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"A labelText and dimBackground are required."]; 25 | [self.commandDelegate sendPluginResult:res callbackId:command.callbackId]; 26 | return; 27 | } 28 | 29 | // Obtain the arguments. 30 | NSString* labelText = [command.arguments objectAtIndex:0]; 31 | BOOL dimBackground = [[command argumentAtIndex:1] boolValue]; 32 | 33 | // Ensure any previous dialogs are closed first. 34 | if (progressIndicator) { 35 | [progressIndicator hide:YES]; 36 | progressIndicator = nil; 37 | } 38 | 39 | progressIndicator = nil; 40 | progressIndicator = [MBProgressHUD showHUDAddedTo:self.webView.superview animated:YES]; 41 | progressIndicator.mode = MBProgressHUDModeIndeterminate; 42 | progressIndicator.dimBackground = dimBackground; 43 | 44 | // If an optional label value was provided, use it. 45 | if (labelText) { 46 | progressIndicator.labelText = labelText; 47 | } 48 | 49 | CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; 50 | [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 51 | } 52 | 53 | - (void)activityStop:(CDVInvokedUrlCommand *)command { 54 | 55 | // If the progress indicator wasn't visible, there is nothing to do. 56 | if (!progressIndicator) { 57 | CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; 58 | [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 59 | return; 60 | } 61 | 62 | // Hide the progress indicator and nil out the reference. 63 | [progressIndicator hide:YES]; 64 | progressIndicator = nil; 65 | 66 | CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@""]; 67 | [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 68 | } 69 | 70 | @end 71 | -------------------------------------------------------------------------------- /src/android/SpinnerPlugin.java: -------------------------------------------------------------------------------- 1 | package net.justin_credible.cordova; 2 | 3 | import android.app.AlertDialog; 4 | import android.app.ProgressDialog; 5 | import android.graphics.drawable.ColorDrawable; 6 | 7 | import net.justin_credible.cordova.FakeR; 8 | 9 | import org.apache.cordova.CallbackContext; 10 | import org.apache.cordova.CordovaPlugin; 11 | import org.json.JSONArray; 12 | import org.json.JSONException; 13 | 14 | public final class SpinnerPlugin extends CordovaPlugin { 15 | 16 | private FakeR R = null; 17 | private ProgressDialog spinnerDialog = null; 18 | 19 | @Override 20 | public void pluginInitialize() { 21 | super.pluginInitialize(); 22 | 23 | R = new FakeR(this.cordova.getActivity()); 24 | } 25 | 26 | @Override 27 | public synchronized boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException { 28 | 29 | if (action == null) { 30 | return false; 31 | } 32 | 33 | if (action.equals("activityStart")) { 34 | 35 | try { 36 | this.activityStart(args, callbackContext); 37 | } 38 | catch (Exception exception) { 39 | callbackContext.error("SpinnerPlugin uncaught exception: " + exception.getMessage()); 40 | } 41 | 42 | return true; 43 | } 44 | else if (action.equals("activityStop")) { 45 | 46 | try { 47 | this.activityStop(callbackContext); 48 | } 49 | catch (Exception exception) { 50 | callbackContext.error("SpinnerPlugin uncaught exception: " + exception.getMessage()); 51 | } 52 | 53 | return true; 54 | } 55 | else { 56 | // The given action was not handled above. 57 | return false; 58 | } 59 | } 60 | 61 | private synchronized void activityStart(final JSONArray args, final CallbackContext callbackContext) throws JSONException { 62 | 63 | // Ensure we have the correct number of arguments. 64 | if (args.length() != 2) { 65 | callbackContext.error("A labelText and dimBackground are required."); 66 | return; 67 | } 68 | 69 | // Obtain the arguments. 70 | final String labelText = args.getString(0); 71 | final boolean dimBackground = args.getBoolean(1); 72 | 73 | // Ensure any previous dialogs are closed first. 74 | if (this.spinnerDialog != null) { 75 | this.spinnerDialog.dismiss(); 76 | this.spinnerDialog = null; 77 | } 78 | 79 | if (dimBackground == false) { 80 | // If we aren't dimming the background, we need to use a special theme to hide it. 81 | int theme = R.getId("style", "SpinnerPluginTransparentDialog"); 82 | this.spinnerDialog = new ProgressDialog(this.cordova.getActivity(), theme); 83 | } 84 | else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 85 | // Use a more modern looking dialog if the platform supports it. 86 | this.spinnerDialog = new ProgressDialog(this.cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT); 87 | } 88 | else { 89 | // Fallback to the platform default. 90 | this.spinnerDialog = new ProgressDialog(this.cordova.getActivity()); 91 | } 92 | 93 | this.spinnerDialog.setMessage(labelText); 94 | this.spinnerDialog.setIndeterminate(true); 95 | this.spinnerDialog.setCancelable(false); 96 | this.spinnerDialog.show(); 97 | 98 | callbackContext.success(); 99 | } 100 | 101 | private synchronized void activityStop(final CallbackContext callbackContext) throws JSONException { 102 | 103 | // Hide the dialog and null out the reference. 104 | if (this.spinnerDialog != null) { 105 | this.spinnerDialog.dismiss(); 106 | this.spinnerDialog = null; 107 | } 108 | 109 | callbackContext.success(); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/ios/MBProgressHUD.h: -------------------------------------------------------------------------------- 1 | // 2 | // MBProgressHUD.h 3 | // Version 0.9.1 4 | // Created by Matej Bukovinski on 2.4.09. 5 | // 6 | 7 | // This code is distributed under the terms and conditions of the MIT license. 8 | 9 | // Copyright (c) 2009-2015 Matej Bukovinski 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining a copy 12 | // of this software and associated documentation files (the "Software"), to deal 13 | // in the Software without restriction, including without limitation the rights 14 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15 | // copies of the Software, and to permit persons to whom the Software is 16 | // furnished to do so, subject to the following conditions: 17 | // 18 | // The above copyright notice and this permission notice shall be included in 19 | // all copies or substantial portions of the Software. 20 | // 21 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 27 | // THE SOFTWARE. 28 | 29 | #import 30 | #import 31 | #import 32 | 33 | @protocol MBProgressHUDDelegate; 34 | 35 | 36 | typedef NS_ENUM(NSInteger, MBProgressHUDMode) { 37 | /** Progress is shown using an UIActivityIndicatorView. This is the default. */ 38 | MBProgressHUDModeIndeterminate, 39 | /** Progress is shown using a round, pie-chart like, progress view. */ 40 | MBProgressHUDModeDeterminate, 41 | /** Progress is shown using a horizontal progress bar */ 42 | MBProgressHUDModeDeterminateHorizontalBar, 43 | /** Progress is shown using a ring-shaped progress view. */ 44 | MBProgressHUDModeAnnularDeterminate, 45 | /** Shows a custom view */ 46 | MBProgressHUDModeCustomView, 47 | /** Shows only labels */ 48 | MBProgressHUDModeText 49 | }; 50 | 51 | typedef NS_ENUM(NSInteger, MBProgressHUDAnimation) { 52 | /** Opacity animation */ 53 | MBProgressHUDAnimationFade, 54 | /** Opacity + scale animation */ 55 | MBProgressHUDAnimationZoom, 56 | MBProgressHUDAnimationZoomOut = MBProgressHUDAnimationZoom, 57 | MBProgressHUDAnimationZoomIn 58 | }; 59 | 60 | 61 | #ifndef MB_INSTANCETYPE 62 | #if __has_feature(objc_instancetype) 63 | #define MB_INSTANCETYPE instancetype 64 | #else 65 | #define MB_INSTANCETYPE id 66 | #endif 67 | #endif 68 | 69 | #ifndef MB_STRONG 70 | #if __has_feature(objc_arc) 71 | #define MB_STRONG strong 72 | #else 73 | #define MB_STRONG retain 74 | #endif 75 | #endif 76 | 77 | #ifndef MB_WEAK 78 | #if __has_feature(objc_arc_weak) 79 | #define MB_WEAK weak 80 | #elif __has_feature(objc_arc) 81 | #define MB_WEAK unsafe_unretained 82 | #else 83 | #define MB_WEAK assign 84 | #endif 85 | #endif 86 | 87 | #if NS_BLOCKS_AVAILABLE 88 | typedef void (^MBProgressHUDCompletionBlock)(); 89 | #endif 90 | 91 | 92 | /** 93 | * Displays a simple HUD window containing a progress indicator and two optional labels for short messages. 94 | * 95 | * This is a simple drop-in class for displaying a progress HUD view similar to Apple's private UIProgressHUD class. 96 | * The MBProgressHUD window spans over the entire space given to it by the initWithFrame constructor and catches all 97 | * user input on this region, thereby preventing the user operations on components below the view. The HUD itself is 98 | * drawn centered as a rounded semi-transparent view which resizes depending on the user specified content. 99 | * 100 | * This view supports four modes of operation: 101 | * - MBProgressHUDModeIndeterminate - shows a UIActivityIndicatorView 102 | * - MBProgressHUDModeDeterminate - shows a custom round progress indicator 103 | * - MBProgressHUDModeAnnularDeterminate - shows a custom annular progress indicator 104 | * - MBProgressHUDModeCustomView - shows an arbitrary, user specified view (see `customView`) 105 | * 106 | * All three modes can have optional labels assigned: 107 | * - If the labelText property is set and non-empty then a label containing the provided content is placed below the 108 | * indicator view. 109 | * - If also the detailsLabelText property is set then another label is placed below the first label. 110 | */ 111 | @interface MBProgressHUD : UIView 112 | 113 | /** 114 | * Creates a new HUD, adds it to provided view and shows it. The counterpart to this method is hideHUDForView:animated:. 115 | * 116 | * @note This method sets `removeFromSuperViewOnHide`. The HUD will automatically be removed from the view hierarchy when hidden. 117 | * 118 | * @param view The view that the HUD will be added to 119 | * @param animated If set to YES the HUD will appear using the current animationType. If set to NO the HUD will not use 120 | * animations while appearing. 121 | * @return A reference to the created HUD. 122 | * 123 | * @see hideHUDForView:animated: 124 | * @see animationType 125 | */ 126 | + (MB_INSTANCETYPE)showHUDAddedTo:(UIView *)view animated:(BOOL)animated; 127 | 128 | /** 129 | * Finds the top-most HUD subview and hides it. The counterpart to this method is showHUDAddedTo:animated:. 130 | * 131 | * @note This method sets `removeFromSuperViewOnHide`. The HUD will automatically be removed from the view hierarchy when hidden. 132 | * 133 | * @param view The view that is going to be searched for a HUD subview. 134 | * @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use 135 | * animations while disappearing. 136 | * @return YES if a HUD was found and removed, NO otherwise. 137 | * 138 | * @see showHUDAddedTo:animated: 139 | * @see animationType 140 | */ 141 | + (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated; 142 | 143 | /** 144 | * Finds all the HUD subviews and hides them. 145 | * 146 | * @note This method sets `removeFromSuperViewOnHide`. The HUDs will automatically be removed from the view hierarchy when hidden. 147 | * 148 | * @param view The view that is going to be searched for HUD subviews. 149 | * @param animated If set to YES the HUDs will disappear using the current animationType. If set to NO the HUDs will not use 150 | * animations while disappearing. 151 | * @return the number of HUDs found and removed. 152 | * 153 | * @see hideHUDForView:animated: 154 | * @see animationType 155 | */ 156 | + (NSUInteger)hideAllHUDsForView:(UIView *)view animated:(BOOL)animated; 157 | 158 | /** 159 | * Finds the top-most HUD subview and returns it. 160 | * 161 | * @param view The view that is going to be searched. 162 | * @return A reference to the last HUD subview discovered. 163 | */ 164 | + (MB_INSTANCETYPE)HUDForView:(UIView *)view; 165 | 166 | /** 167 | * Finds all HUD subviews and returns them. 168 | * 169 | * @param view The view that is going to be searched. 170 | * @return All found HUD views (array of MBProgressHUD objects). 171 | */ 172 | + (NSArray *)allHUDsForView:(UIView *)view; 173 | 174 | /** 175 | * A convenience constructor that initializes the HUD with the window's bounds. Calls the designated constructor with 176 | * window.bounds as the parameter. 177 | * 178 | * @param window The window instance that will provide the bounds for the HUD. Should be the same instance as 179 | * the HUD's superview (i.e., the window that the HUD will be added to). 180 | */ 181 | - (id)initWithWindow:(UIWindow *)window; 182 | 183 | /** 184 | * A convenience constructor that initializes the HUD with the view's bounds. Calls the designated constructor with 185 | * view.bounds as the parameter 186 | * 187 | * @param view The view instance that will provide the bounds for the HUD. Should be the same instance as 188 | * the HUD's superview (i.e., the view that the HUD will be added to). 189 | */ 190 | - (id)initWithView:(UIView *)view; 191 | 192 | /** 193 | * Display the HUD. You need to make sure that the main thread completes its run loop soon after this method call so 194 | * the user interface can be updated. Call this method when your task is already set-up to be executed in a new thread 195 | * (e.g., when using something like NSOperation or calling an asynchronous call like NSURLRequest). 196 | * 197 | * @param animated If set to YES the HUD will appear using the current animationType. If set to NO the HUD will not use 198 | * animations while appearing. 199 | * 200 | * @see animationType 201 | */ 202 | - (void)show:(BOOL)animated; 203 | 204 | /** 205 | * Hide the HUD. This still calls the hudWasHidden: delegate. This is the counterpart of the show: method. Use it to 206 | * hide the HUD when your task completes. 207 | * 208 | * @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use 209 | * animations while disappearing. 210 | * 211 | * @see animationType 212 | */ 213 | - (void)hide:(BOOL)animated; 214 | 215 | /** 216 | * Hide the HUD after a delay. This still calls the hudWasHidden: delegate. This is the counterpart of the show: method. Use it to 217 | * hide the HUD when your task completes. 218 | * 219 | * @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use 220 | * animations while disappearing. 221 | * @param delay Delay in seconds until the HUD is hidden. 222 | * 223 | * @see animationType 224 | */ 225 | - (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay; 226 | 227 | /** 228 | * Shows the HUD while a background task is executing in a new thread, then hides the HUD. 229 | * 230 | * This method also takes care of autorelease pools so your method does not have to be concerned with setting up a 231 | * pool. 232 | * 233 | * @param method The method to be executed while the HUD is shown. This method will be executed in a new thread. 234 | * @param target The object that the target method belongs to. 235 | * @param object An optional object to be passed to the method. 236 | * @param animated If set to YES the HUD will (dis)appear using the current animationType. If set to NO the HUD will not use 237 | * animations while (dis)appearing. 238 | */ 239 | - (void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object animated:(BOOL)animated; 240 | 241 | #if NS_BLOCKS_AVAILABLE 242 | 243 | /** 244 | * Shows the HUD while a block is executing on a background queue, then hides the HUD. 245 | * 246 | * @see showAnimated:whileExecutingBlock:onQueue:completionBlock: 247 | */ 248 | - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block; 249 | 250 | /** 251 | * Shows the HUD while a block is executing on a background queue, then hides the HUD. 252 | * 253 | * @see showAnimated:whileExecutingBlock:onQueue:completionBlock: 254 | */ 255 | - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block completionBlock:(MBProgressHUDCompletionBlock)completion; 256 | 257 | /** 258 | * Shows the HUD while a block is executing on the specified dispatch queue, then hides the HUD. 259 | * 260 | * @see showAnimated:whileExecutingBlock:onQueue:completionBlock: 261 | */ 262 | - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue; 263 | 264 | /** 265 | * Shows the HUD while a block is executing on the specified dispatch queue, executes completion block on the main queue, and then hides the HUD. 266 | * 267 | * @param animated If set to YES the HUD will (dis)appear using the current animationType. If set to NO the HUD will 268 | * not use animations while (dis)appearing. 269 | * @param block The block to be executed while the HUD is shown. 270 | * @param queue The dispatch queue on which the block should be executed. 271 | * @param completion The block to be executed on completion. 272 | * 273 | * @see completionBlock 274 | */ 275 | - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue 276 | completionBlock:(MBProgressHUDCompletionBlock)completion; 277 | 278 | /** 279 | * A block that gets called after the HUD was completely hidden. 280 | */ 281 | @property (copy) MBProgressHUDCompletionBlock completionBlock; 282 | 283 | #endif 284 | 285 | /** 286 | * MBProgressHUD operation mode. The default is MBProgressHUDModeIndeterminate. 287 | * 288 | * @see MBProgressHUDMode 289 | */ 290 | @property (assign) MBProgressHUDMode mode; 291 | 292 | /** 293 | * The animation type that should be used when the HUD is shown and hidden. 294 | * 295 | * @see MBProgressHUDAnimation 296 | */ 297 | @property (assign) MBProgressHUDAnimation animationType; 298 | 299 | /** 300 | * The UIView (e.g., a UIImageView) to be shown when the HUD is in MBProgressHUDModeCustomView. 301 | * For best results use a 37 by 37 pixel view (so the bounds match the built in indicator bounds). 302 | */ 303 | @property (MB_STRONG) UIView *customView; 304 | 305 | /** 306 | * The HUD delegate object. 307 | * 308 | * @see MBProgressHUDDelegate 309 | */ 310 | @property (MB_WEAK) id delegate; 311 | 312 | /** 313 | * An optional short message to be displayed below the activity indicator. The HUD is automatically resized to fit 314 | * the entire text. If the text is too long it will get clipped by displaying "..." at the end. If left unchanged or 315 | * set to @"", then no message is displayed. 316 | */ 317 | @property (copy) NSString *labelText; 318 | 319 | /** 320 | * An optional details message displayed below the labelText message. This message is displayed only if the labelText 321 | * property is also set and is different from an empty string (@""). The details text can span multiple lines. 322 | */ 323 | @property (copy) NSString *detailsLabelText; 324 | 325 | /** 326 | * The opacity of the HUD window. Defaults to 0.8 (80% opacity). 327 | */ 328 | @property (assign) float opacity; 329 | 330 | /** 331 | * The color of the HUD window. Defaults to black. If this property is set, color is set using 332 | * this UIColor and the opacity property is not used. using retain because performing copy on 333 | * UIColor base colors (like [UIColor greenColor]) cause problems with the copyZone. 334 | */ 335 | @property (MB_STRONG) UIColor *color; 336 | 337 | /** 338 | * The x-axis offset of the HUD relative to the centre of the superview. 339 | */ 340 | @property (assign) float xOffset; 341 | 342 | /** 343 | * The y-axis offset of the HUD relative to the centre of the superview. 344 | */ 345 | @property (assign) float yOffset; 346 | 347 | /** 348 | * The amount of space between the HUD edge and the HUD elements (labels, indicators or custom views). 349 | * Defaults to 20.0 350 | */ 351 | @property (assign) float margin; 352 | 353 | /** 354 | * The corner radius for the HUD 355 | * Defaults to 10.0 356 | */ 357 | @property (assign) float cornerRadius; 358 | 359 | /** 360 | * Cover the HUD background view with a radial gradient. 361 | */ 362 | @property (assign) BOOL dimBackground; 363 | 364 | /* 365 | * Grace period is the time (in seconds) that the invoked method may be run without 366 | * showing the HUD. If the task finishes before the grace time runs out, the HUD will 367 | * not be shown at all. 368 | * This may be used to prevent HUD display for very short tasks. 369 | * Defaults to 0 (no grace time). 370 | * Grace time functionality is only supported when the task status is known! 371 | * @see taskInProgress 372 | */ 373 | @property (assign) float graceTime; 374 | 375 | /** 376 | * The minimum time (in seconds) that the HUD is shown. 377 | * This avoids the problem of the HUD being shown and than instantly hidden. 378 | * Defaults to 0 (no minimum show time). 379 | */ 380 | @property (assign) float minShowTime; 381 | 382 | /** 383 | * Indicates that the executed operation is in progress. Needed for correct graceTime operation. 384 | * If you don't set a graceTime (different than 0.0) this does nothing. 385 | * This property is automatically set when using showWhileExecuting:onTarget:withObject:animated:. 386 | * When threading is done outside of the HUD (i.e., when the show: and hide: methods are used directly), 387 | * you need to set this property when your task starts and completes in order to have normal graceTime 388 | * functionality. 389 | */ 390 | @property (assign) BOOL taskInProgress; 391 | 392 | /** 393 | * Removes the HUD from its parent view when hidden. 394 | * Defaults to NO. 395 | */ 396 | @property (assign) BOOL removeFromSuperViewOnHide; 397 | 398 | /** 399 | * Font to be used for the main label. Set this property if the default is not adequate. 400 | */ 401 | @property (MB_STRONG) UIFont* labelFont; 402 | 403 | /** 404 | * Color to be used for the main label. Set this property if the default is not adequate. 405 | */ 406 | @property (MB_STRONG) UIColor* labelColor; 407 | 408 | /** 409 | * Font to be used for the details label. Set this property if the default is not adequate. 410 | */ 411 | @property (MB_STRONG) UIFont* detailsLabelFont; 412 | 413 | /** 414 | * Color to be used for the details label. Set this property if the default is not adequate. 415 | */ 416 | @property (MB_STRONG) UIColor* detailsLabelColor; 417 | 418 | /** 419 | * The color of the activity indicator. Defaults to [UIColor whiteColor] 420 | * Does nothing on pre iOS 5. 421 | */ 422 | @property (MB_STRONG) UIColor *activityIndicatorColor; 423 | 424 | /** 425 | * The progress of the progress indicator, from 0.0 to 1.0. Defaults to 0.0. 426 | */ 427 | @property (assign) float progress; 428 | 429 | /** 430 | * The minimum size of the HUD bezel. Defaults to CGSizeZero (no minimum size). 431 | */ 432 | @property (assign) CGSize minSize; 433 | 434 | 435 | /** 436 | * The actual size of the HUD bezel. 437 | * You can use this to limit touch handling on the bezel aria only. 438 | * @see https://github.com/jdg/MBProgressHUD/pull/200 439 | */ 440 | @property (atomic, assign, readonly) CGSize size; 441 | 442 | 443 | /** 444 | * Force the HUD dimensions to be equal if possible. 445 | */ 446 | @property (assign, getter = isSquare) BOOL square; 447 | 448 | @end 449 | 450 | 451 | @protocol MBProgressHUDDelegate 452 | 453 | @optional 454 | 455 | /** 456 | * Called after the HUD was fully hidden from the screen. 457 | */ 458 | - (void)hudWasHidden:(MBProgressHUD *)hud; 459 | 460 | @end 461 | 462 | 463 | /** 464 | * A progress view for showing definite progress by filling up a circle (pie chart). 465 | */ 466 | @interface MBRoundProgressView : UIView 467 | 468 | /** 469 | * Progress (0.0 to 1.0) 470 | */ 471 | @property (nonatomic, assign) float progress; 472 | 473 | /** 474 | * Indicator progress color. 475 | * Defaults to white [UIColor whiteColor] 476 | */ 477 | @property (nonatomic, MB_STRONG) UIColor *progressTintColor; 478 | 479 | /** 480 | * Indicator background (non-progress) color. 481 | * Defaults to translucent white (alpha 0.1) 482 | */ 483 | @property (nonatomic, MB_STRONG) UIColor *backgroundTintColor; 484 | 485 | /* 486 | * Display mode - NO = round or YES = annular. Defaults to round. 487 | */ 488 | @property (nonatomic, assign, getter = isAnnular) BOOL annular; 489 | 490 | @end 491 | 492 | 493 | /** 494 | * A flat bar progress view. 495 | */ 496 | @interface MBBarProgressView : UIView 497 | 498 | /** 499 | * Progress (0.0 to 1.0) 500 | */ 501 | @property (nonatomic, assign) float progress; 502 | 503 | /** 504 | * Bar border line color. 505 | * Defaults to white [UIColor whiteColor]. 506 | */ 507 | @property (nonatomic, MB_STRONG) UIColor *lineColor; 508 | 509 | /** 510 | * Bar background color. 511 | * Defaults to clear [UIColor clearColor]; 512 | */ 513 | @property (nonatomic, MB_STRONG) UIColor *progressRemainingColor; 514 | 515 | /** 516 | * Bar progress color. 517 | * Defaults to white [UIColor whiteColor]. 518 | */ 519 | @property (nonatomic, MB_STRONG) UIColor *progressColor; 520 | 521 | @end 522 | -------------------------------------------------------------------------------- /src/ios/MBProgressHUD.m: -------------------------------------------------------------------------------- 1 | // 2 | // MBProgressHUD.m 3 | // Version 0.9.1 4 | // Created by Matej Bukovinski on 2.4.09. 5 | // 6 | 7 | #import "MBProgressHUD.h" 8 | #import 9 | 10 | 11 | #if __has_feature(objc_arc) 12 | #define MB_AUTORELEASE(exp) exp 13 | #define MB_RELEASE(exp) exp 14 | #define MB_RETAIN(exp) exp 15 | #else 16 | #define MB_AUTORELEASE(exp) [exp autorelease] 17 | #define MB_RELEASE(exp) [exp release] 18 | #define MB_RETAIN(exp) [exp retain] 19 | #endif 20 | 21 | #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000 22 | #define MBLabelAlignmentCenter NSTextAlignmentCenter 23 | #else 24 | #define MBLabelAlignmentCenter UITextAlignmentCenter 25 | #endif 26 | 27 | #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000 28 | #define MB_TEXTSIZE(text, font) [text length] > 0 ? [text \ 29 | sizeWithAttributes:@{NSFontAttributeName:font}] : CGSizeZero; 30 | #else 31 | #define MB_TEXTSIZE(text, font) [text length] > 0 ? [text sizeWithFont:font] : CGSizeZero; 32 | #endif 33 | 34 | #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000 35 | #define MB_MULTILINE_TEXTSIZE(text, font, maxSize, mode) [text length] > 0 ? [text \ 36 | boundingRectWithSize:maxSize options:(NSStringDrawingUsesLineFragmentOrigin) \ 37 | attributes:@{NSFontAttributeName:font} context:nil].size : CGSizeZero; 38 | #else 39 | #define MB_MULTILINE_TEXTSIZE(text, font, maxSize, mode) [text length] > 0 ? [text \ 40 | sizeWithFont:font constrainedToSize:maxSize lineBreakMode:mode] : CGSizeZero; 41 | #endif 42 | 43 | #ifndef kCFCoreFoundationVersionNumber_iOS_7_0 44 | #define kCFCoreFoundationVersionNumber_iOS_7_0 847.20 45 | #endif 46 | 47 | #ifndef kCFCoreFoundationVersionNumber_iOS_8_0 48 | #define kCFCoreFoundationVersionNumber_iOS_8_0 1129.15 49 | #endif 50 | 51 | 52 | static const CGFloat kPadding = 4.f; 53 | static const CGFloat kLabelFontSize = 16.f; 54 | static const CGFloat kDetailsLabelFontSize = 12.f; 55 | 56 | 57 | @interface MBProgressHUD () { 58 | BOOL useAnimation; 59 | SEL methodForExecution; 60 | id targetForExecution; 61 | id objectForExecution; 62 | UILabel *label; 63 | UILabel *detailsLabel; 64 | BOOL isFinished; 65 | CGAffineTransform rotationTransform; 66 | } 67 | 68 | @property (atomic, MB_STRONG) UIView *indicator; 69 | @property (atomic, MB_STRONG) NSTimer *graceTimer; 70 | @property (atomic, MB_STRONG) NSTimer *minShowTimer; 71 | @property (atomic, MB_STRONG) NSDate *showStarted; 72 | 73 | @end 74 | 75 | 76 | @implementation MBProgressHUD 77 | 78 | #pragma mark - Properties 79 | 80 | @synthesize animationType; 81 | @synthesize delegate; 82 | @synthesize opacity; 83 | @synthesize color; 84 | @synthesize labelFont; 85 | @synthesize labelColor; 86 | @synthesize detailsLabelFont; 87 | @synthesize detailsLabelColor; 88 | @synthesize indicator; 89 | @synthesize xOffset; 90 | @synthesize yOffset; 91 | @synthesize minSize; 92 | @synthesize square; 93 | @synthesize margin; 94 | @synthesize dimBackground; 95 | @synthesize graceTime; 96 | @synthesize minShowTime; 97 | @synthesize graceTimer; 98 | @synthesize minShowTimer; 99 | @synthesize taskInProgress; 100 | @synthesize removeFromSuperViewOnHide; 101 | @synthesize customView; 102 | @synthesize showStarted; 103 | @synthesize mode; 104 | @synthesize labelText; 105 | @synthesize detailsLabelText; 106 | @synthesize progress; 107 | @synthesize size; 108 | @synthesize activityIndicatorColor; 109 | #if NS_BLOCKS_AVAILABLE 110 | @synthesize completionBlock; 111 | #endif 112 | 113 | #pragma mark - Class methods 114 | 115 | + (MB_INSTANCETYPE)showHUDAddedTo:(UIView *)view animated:(BOOL)animated { 116 | MBProgressHUD *hud = [[self alloc] initWithView:view]; 117 | hud.removeFromSuperViewOnHide = YES; 118 | [view addSubview:hud]; 119 | [hud show:animated]; 120 | return MB_AUTORELEASE(hud); 121 | } 122 | 123 | + (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated { 124 | MBProgressHUD *hud = [self HUDForView:view]; 125 | if (hud != nil) { 126 | hud.removeFromSuperViewOnHide = YES; 127 | [hud hide:animated]; 128 | return YES; 129 | } 130 | return NO; 131 | } 132 | 133 | + (NSUInteger)hideAllHUDsForView:(UIView *)view animated:(BOOL)animated { 134 | NSArray *huds = [MBProgressHUD allHUDsForView:view]; 135 | for (MBProgressHUD *hud in huds) { 136 | hud.removeFromSuperViewOnHide = YES; 137 | [hud hide:animated]; 138 | } 139 | return [huds count]; 140 | } 141 | 142 | + (MB_INSTANCETYPE)HUDForView:(UIView *)view { 143 | NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator]; 144 | for (UIView *subview in subviewsEnum) { 145 | if ([subview isKindOfClass:self]) { 146 | return (MBProgressHUD *)subview; 147 | } 148 | } 149 | return nil; 150 | } 151 | 152 | + (NSArray *)allHUDsForView:(UIView *)view { 153 | NSMutableArray *huds = [NSMutableArray array]; 154 | NSArray *subviews = view.subviews; 155 | for (UIView *aView in subviews) { 156 | if ([aView isKindOfClass:self]) { 157 | [huds addObject:aView]; 158 | } 159 | } 160 | return [NSArray arrayWithArray:huds]; 161 | } 162 | 163 | #pragma mark - Lifecycle 164 | 165 | - (id)initWithFrame:(CGRect)frame { 166 | self = [super initWithFrame:frame]; 167 | if (self) { 168 | // Set default values for properties 169 | self.animationType = MBProgressHUDAnimationFade; 170 | self.mode = MBProgressHUDModeIndeterminate; 171 | self.labelText = nil; 172 | self.detailsLabelText = nil; 173 | self.opacity = 0.8f; 174 | self.color = nil; 175 | self.labelFont = [UIFont boldSystemFontOfSize:kLabelFontSize]; 176 | self.labelColor = [UIColor whiteColor]; 177 | self.detailsLabelFont = [UIFont boldSystemFontOfSize:kDetailsLabelFontSize]; 178 | self.detailsLabelColor = [UIColor whiteColor]; 179 | self.activityIndicatorColor = [UIColor whiteColor]; 180 | self.xOffset = 0.0f; 181 | self.yOffset = 0.0f; 182 | self.dimBackground = NO; 183 | self.margin = 20.0f; 184 | self.cornerRadius = 10.0f; 185 | self.graceTime = 0.0f; 186 | self.minShowTime = 0.0f; 187 | self.removeFromSuperViewOnHide = NO; 188 | self.minSize = CGSizeZero; 189 | self.square = NO; 190 | self.contentMode = UIViewContentModeCenter; 191 | self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin 192 | | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 193 | 194 | // Transparent background 195 | self.opaque = NO; 196 | self.backgroundColor = [UIColor clearColor]; 197 | // Make it invisible for now 198 | self.alpha = 0.0f; 199 | 200 | taskInProgress = NO; 201 | rotationTransform = CGAffineTransformIdentity; 202 | 203 | [self setupLabels]; 204 | [self updateIndicators]; 205 | [self registerForKVO]; 206 | [self registerForNotifications]; 207 | } 208 | return self; 209 | } 210 | 211 | - (id)initWithView:(UIView *)view { 212 | NSAssert(view, @"View must not be nil."); 213 | return [self initWithFrame:view.bounds]; 214 | } 215 | 216 | - (id)initWithWindow:(UIWindow *)window { 217 | return [self initWithView:window]; 218 | } 219 | 220 | - (void)dealloc { 221 | [self unregisterFromNotifications]; 222 | [self unregisterFromKVO]; 223 | #if !__has_feature(objc_arc) 224 | [color release]; 225 | [indicator release]; 226 | [label release]; 227 | [detailsLabel release]; 228 | [labelText release]; 229 | [detailsLabelText release]; 230 | [graceTimer release]; 231 | [minShowTimer release]; 232 | [showStarted release]; 233 | [customView release]; 234 | [labelFont release]; 235 | [labelColor release]; 236 | [detailsLabelFont release]; 237 | [detailsLabelColor release]; 238 | #if NS_BLOCKS_AVAILABLE 239 | [completionBlock release]; 240 | #endif 241 | [super dealloc]; 242 | #endif 243 | } 244 | 245 | #pragma mark - Show & hide 246 | 247 | - (void)show:(BOOL)animated { 248 | NSAssert([NSThread isMainThread], @"MBProgressHUD needs to be accessed on the main thread."); 249 | useAnimation = animated; 250 | // If the grace time is set postpone the HUD display 251 | if (self.graceTime > 0.0) { 252 | NSTimer *newGraceTimer = [NSTimer timerWithTimeInterval:self.graceTime target:self selector:@selector(handleGraceTimer:) userInfo:nil repeats:NO]; 253 | [[NSRunLoop currentRunLoop] addTimer:newGraceTimer forMode:NSRunLoopCommonModes]; 254 | self.graceTimer = newGraceTimer; 255 | } 256 | // ... otherwise show the HUD imediately 257 | else { 258 | [self showUsingAnimation:useAnimation]; 259 | } 260 | } 261 | 262 | - (void)hide:(BOOL)animated { 263 | NSAssert([NSThread isMainThread], @"MBProgressHUD needs to be accessed on the main thread."); 264 | useAnimation = animated; 265 | // If the minShow time is set, calculate how long the hud was shown, 266 | // and pospone the hiding operation if necessary 267 | if (self.minShowTime > 0.0 && showStarted) { 268 | NSTimeInterval interv = [[NSDate date] timeIntervalSinceDate:showStarted]; 269 | if (interv < self.minShowTime) { 270 | self.minShowTimer = [NSTimer scheduledTimerWithTimeInterval:(self.minShowTime - interv) target:self 271 | selector:@selector(handleMinShowTimer:) userInfo:nil repeats:NO]; 272 | return; 273 | } 274 | } 275 | // ... otherwise hide the HUD immediately 276 | [self hideUsingAnimation:useAnimation]; 277 | } 278 | 279 | - (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay { 280 | [self performSelector:@selector(hideDelayed:) withObject:[NSNumber numberWithBool:animated] afterDelay:delay]; 281 | } 282 | 283 | - (void)hideDelayed:(NSNumber *)animated { 284 | [self hide:[animated boolValue]]; 285 | } 286 | 287 | #pragma mark - Timer callbacks 288 | 289 | - (void)handleGraceTimer:(NSTimer *)theTimer { 290 | // Show the HUD only if the task is still running 291 | if (taskInProgress) { 292 | [self showUsingAnimation:useAnimation]; 293 | } 294 | } 295 | 296 | - (void)handleMinShowTimer:(NSTimer *)theTimer { 297 | [self hideUsingAnimation:useAnimation]; 298 | } 299 | 300 | #pragma mark - View Hierrarchy 301 | 302 | - (void)didMoveToSuperview { 303 | [self updateForCurrentOrientationAnimated:NO]; 304 | } 305 | 306 | #pragma mark - Internal show & hide operations 307 | 308 | - (void)showUsingAnimation:(BOOL)animated { 309 | // Cancel any scheduled hideDelayed: calls 310 | [NSObject cancelPreviousPerformRequestsWithTarget:self]; 311 | [self setNeedsDisplay]; 312 | 313 | if (animated && animationType == MBProgressHUDAnimationZoomIn) { 314 | self.transform = CGAffineTransformConcat(rotationTransform, CGAffineTransformMakeScale(0.5f, 0.5f)); 315 | } else if (animated && animationType == MBProgressHUDAnimationZoomOut) { 316 | self.transform = CGAffineTransformConcat(rotationTransform, CGAffineTransformMakeScale(1.5f, 1.5f)); 317 | } 318 | self.showStarted = [NSDate date]; 319 | // Fade in 320 | if (animated) { 321 | [UIView beginAnimations:nil context:NULL]; 322 | [UIView setAnimationDuration:0.30]; 323 | self.alpha = 1.0f; 324 | if (animationType == MBProgressHUDAnimationZoomIn || animationType == MBProgressHUDAnimationZoomOut) { 325 | self.transform = rotationTransform; 326 | } 327 | [UIView commitAnimations]; 328 | } 329 | else { 330 | self.alpha = 1.0f; 331 | } 332 | } 333 | 334 | - (void)hideUsingAnimation:(BOOL)animated { 335 | // Fade out 336 | if (animated && showStarted) { 337 | [UIView beginAnimations:nil context:NULL]; 338 | [UIView setAnimationDuration:0.30]; 339 | [UIView setAnimationDelegate:self]; 340 | [UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)]; 341 | // 0.02 prevents the hud from passing through touches during the animation the hud will get completely hidden 342 | // in the done method 343 | if (animationType == MBProgressHUDAnimationZoomIn) { 344 | self.transform = CGAffineTransformConcat(rotationTransform, CGAffineTransformMakeScale(1.5f, 1.5f)); 345 | } else if (animationType == MBProgressHUDAnimationZoomOut) { 346 | self.transform = CGAffineTransformConcat(rotationTransform, CGAffineTransformMakeScale(0.5f, 0.5f)); 347 | } 348 | 349 | self.alpha = 0.02f; 350 | [UIView commitAnimations]; 351 | } 352 | else { 353 | self.alpha = 0.0f; 354 | [self done]; 355 | } 356 | self.showStarted = nil; 357 | } 358 | 359 | - (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void*)context { 360 | [self done]; 361 | } 362 | 363 | - (void)done { 364 | [NSObject cancelPreviousPerformRequestsWithTarget:self]; 365 | isFinished = YES; 366 | self.alpha = 0.0f; 367 | if (removeFromSuperViewOnHide) { 368 | [self removeFromSuperview]; 369 | } 370 | #if NS_BLOCKS_AVAILABLE 371 | if (self.completionBlock) { 372 | self.completionBlock(); 373 | self.completionBlock = NULL; 374 | } 375 | #endif 376 | if ([delegate respondsToSelector:@selector(hudWasHidden:)]) { 377 | [delegate performSelector:@selector(hudWasHidden:) withObject:self]; 378 | } 379 | } 380 | 381 | #pragma mark - Threading 382 | 383 | - (void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object animated:(BOOL)animated { 384 | methodForExecution = method; 385 | targetForExecution = MB_RETAIN(target); 386 | objectForExecution = MB_RETAIN(object); 387 | // Launch execution in new thread 388 | self.taskInProgress = YES; 389 | [NSThread detachNewThreadSelector:@selector(launchExecution) toTarget:self withObject:nil]; 390 | // Show HUD view 391 | [self show:animated]; 392 | } 393 | 394 | #if NS_BLOCKS_AVAILABLE 395 | 396 | - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block { 397 | dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 398 | [self showAnimated:animated whileExecutingBlock:block onQueue:queue completionBlock:NULL]; 399 | } 400 | 401 | - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block completionBlock:(void (^)())completion { 402 | dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 403 | [self showAnimated:animated whileExecutingBlock:block onQueue:queue completionBlock:completion]; 404 | } 405 | 406 | - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue { 407 | [self showAnimated:animated whileExecutingBlock:block onQueue:queue completionBlock:NULL]; 408 | } 409 | 410 | - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue 411 | completionBlock:(MBProgressHUDCompletionBlock)completion { 412 | self.taskInProgress = YES; 413 | self.completionBlock = completion; 414 | dispatch_async(queue, ^(void) { 415 | block(); 416 | dispatch_async(dispatch_get_main_queue(), ^(void) { 417 | [self cleanUp]; 418 | }); 419 | }); 420 | [self show:animated]; 421 | } 422 | 423 | #endif 424 | 425 | - (void)launchExecution { 426 | @autoreleasepool { 427 | #pragma clang diagnostic push 428 | #pragma clang diagnostic ignored "-Warc-performSelector-leaks" 429 | // Start executing the requested task 430 | [targetForExecution performSelector:methodForExecution withObject:objectForExecution]; 431 | #pragma clang diagnostic pop 432 | // Task completed, update view in main thread (note: view operations should 433 | // be done only in the main thread) 434 | [self performSelectorOnMainThread:@selector(cleanUp) withObject:nil waitUntilDone:NO]; 435 | } 436 | } 437 | 438 | - (void)cleanUp { 439 | taskInProgress = NO; 440 | #if !__has_feature(objc_arc) 441 | [targetForExecution release]; 442 | [objectForExecution release]; 443 | #else 444 | targetForExecution = nil; 445 | objectForExecution = nil; 446 | #endif 447 | [self hide:useAnimation]; 448 | } 449 | 450 | #pragma mark - UI 451 | 452 | - (void)setupLabels { 453 | label = [[UILabel alloc] initWithFrame:self.bounds]; 454 | label.adjustsFontSizeToFitWidth = NO; 455 | label.textAlignment = MBLabelAlignmentCenter; 456 | label.opaque = NO; 457 | label.backgroundColor = [UIColor clearColor]; 458 | label.textColor = self.labelColor; 459 | label.font = self.labelFont; 460 | label.text = self.labelText; 461 | [self addSubview:label]; 462 | 463 | detailsLabel = [[UILabel alloc] initWithFrame:self.bounds]; 464 | detailsLabel.font = self.detailsLabelFont; 465 | detailsLabel.adjustsFontSizeToFitWidth = NO; 466 | detailsLabel.textAlignment = MBLabelAlignmentCenter; 467 | detailsLabel.opaque = NO; 468 | detailsLabel.backgroundColor = [UIColor clearColor]; 469 | detailsLabel.textColor = self.detailsLabelColor; 470 | detailsLabel.numberOfLines = 0; 471 | detailsLabel.font = self.detailsLabelFont; 472 | detailsLabel.text = self.detailsLabelText; 473 | [self addSubview:detailsLabel]; 474 | } 475 | 476 | - (void)updateIndicators { 477 | 478 | BOOL isActivityIndicator = [indicator isKindOfClass:[UIActivityIndicatorView class]]; 479 | BOOL isRoundIndicator = [indicator isKindOfClass:[MBRoundProgressView class]]; 480 | 481 | if (mode == MBProgressHUDModeIndeterminate) { 482 | if (!isActivityIndicator) { 483 | // Update to indeterminate indicator 484 | [indicator removeFromSuperview]; 485 | self.indicator = MB_AUTORELEASE([[UIActivityIndicatorView alloc] 486 | initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]); 487 | [(UIActivityIndicatorView *)indicator startAnimating]; 488 | [self addSubview:indicator]; 489 | } 490 | #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 50000 491 | [(UIActivityIndicatorView *)indicator setColor:self.activityIndicatorColor]; 492 | #endif 493 | } 494 | else if (mode == MBProgressHUDModeDeterminateHorizontalBar) { 495 | // Update to bar determinate indicator 496 | [indicator removeFromSuperview]; 497 | self.indicator = MB_AUTORELEASE([[MBBarProgressView alloc] init]); 498 | [self addSubview:indicator]; 499 | } 500 | else if (mode == MBProgressHUDModeDeterminate || mode == MBProgressHUDModeAnnularDeterminate) { 501 | if (!isRoundIndicator) { 502 | // Update to determinante indicator 503 | [indicator removeFromSuperview]; 504 | self.indicator = MB_AUTORELEASE([[MBRoundProgressView alloc] init]); 505 | [self addSubview:indicator]; 506 | } 507 | if (mode == MBProgressHUDModeAnnularDeterminate) { 508 | [(MBRoundProgressView *)indicator setAnnular:YES]; 509 | } 510 | } 511 | else if (mode == MBProgressHUDModeCustomView && customView != indicator) { 512 | // Update custom view indicator 513 | [indicator removeFromSuperview]; 514 | self.indicator = customView; 515 | [self addSubview:indicator]; 516 | } else if (mode == MBProgressHUDModeText) { 517 | [indicator removeFromSuperview]; 518 | self.indicator = nil; 519 | } 520 | } 521 | 522 | #pragma mark - Layout 523 | 524 | - (void)layoutSubviews { 525 | [super layoutSubviews]; 526 | 527 | // Entirely cover the parent view 528 | UIView *parent = self.superview; 529 | if (parent) { 530 | self.frame = parent.bounds; 531 | } 532 | CGRect bounds = self.bounds; 533 | 534 | // Determine the total widt and height needed 535 | CGFloat maxWidth = bounds.size.width - 4 * margin; 536 | CGSize totalSize = CGSizeZero; 537 | 538 | CGRect indicatorF = indicator.bounds; 539 | indicatorF.size.width = MIN(indicatorF.size.width, maxWidth); 540 | totalSize.width = MAX(totalSize.width, indicatorF.size.width); 541 | totalSize.height += indicatorF.size.height; 542 | 543 | CGSize labelSize = MB_TEXTSIZE(label.text, label.font); 544 | labelSize.width = MIN(labelSize.width, maxWidth); 545 | totalSize.width = MAX(totalSize.width, labelSize.width); 546 | totalSize.height += labelSize.height; 547 | if (labelSize.height > 0.f && indicatorF.size.height > 0.f) { 548 | totalSize.height += kPadding; 549 | } 550 | 551 | CGFloat remainingHeight = bounds.size.height - totalSize.height - kPadding - 4 * margin; 552 | CGSize maxSize = CGSizeMake(maxWidth, remainingHeight); 553 | CGSize detailsLabelSize = MB_MULTILINE_TEXTSIZE(detailsLabel.text, detailsLabel.font, maxSize, detailsLabel.lineBreakMode); 554 | totalSize.width = MAX(totalSize.width, detailsLabelSize.width); 555 | totalSize.height += detailsLabelSize.height; 556 | if (detailsLabelSize.height > 0.f && (indicatorF.size.height > 0.f || labelSize.height > 0.f)) { 557 | totalSize.height += kPadding; 558 | } 559 | 560 | totalSize.width += 2 * margin; 561 | totalSize.height += 2 * margin; 562 | 563 | // Position elements 564 | CGFloat yPos = round(((bounds.size.height - totalSize.height) / 2)) + margin + yOffset; 565 | CGFloat xPos = xOffset; 566 | indicatorF.origin.y = yPos; 567 | indicatorF.origin.x = round((bounds.size.width - indicatorF.size.width) / 2) + xPos; 568 | indicator.frame = indicatorF; 569 | yPos += indicatorF.size.height; 570 | 571 | if (labelSize.height > 0.f && indicatorF.size.height > 0.f) { 572 | yPos += kPadding; 573 | } 574 | CGRect labelF; 575 | labelF.origin.y = yPos; 576 | labelF.origin.x = round((bounds.size.width - labelSize.width) / 2) + xPos; 577 | labelF.size = labelSize; 578 | label.frame = labelF; 579 | yPos += labelF.size.height; 580 | 581 | if (detailsLabelSize.height > 0.f && (indicatorF.size.height > 0.f || labelSize.height > 0.f)) { 582 | yPos += kPadding; 583 | } 584 | CGRect detailsLabelF; 585 | detailsLabelF.origin.y = yPos; 586 | detailsLabelF.origin.x = round((bounds.size.width - detailsLabelSize.width) / 2) + xPos; 587 | detailsLabelF.size = detailsLabelSize; 588 | detailsLabel.frame = detailsLabelF; 589 | 590 | // Enforce minsize and quare rules 591 | if (square) { 592 | CGFloat max = MAX(totalSize.width, totalSize.height); 593 | if (max <= bounds.size.width - 2 * margin) { 594 | totalSize.width = max; 595 | } 596 | if (max <= bounds.size.height - 2 * margin) { 597 | totalSize.height = max; 598 | } 599 | } 600 | if (totalSize.width < minSize.width) { 601 | totalSize.width = minSize.width; 602 | } 603 | if (totalSize.height < minSize.height) { 604 | totalSize.height = minSize.height; 605 | } 606 | 607 | size = totalSize; 608 | } 609 | 610 | #pragma mark BG Drawing 611 | 612 | - (void)drawRect:(CGRect)rect { 613 | 614 | CGContextRef context = UIGraphicsGetCurrentContext(); 615 | UIGraphicsPushContext(context); 616 | 617 | if (self.dimBackground) { 618 | //Gradient colours 619 | size_t gradLocationsNum = 2; 620 | CGFloat gradLocations[2] = {0.0f, 1.0f}; 621 | CGFloat gradColors[8] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.75f}; 622 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 623 | CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradColors, gradLocations, gradLocationsNum); 624 | CGColorSpaceRelease(colorSpace); 625 | //Gradient center 626 | CGPoint gradCenter= CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2); 627 | //Gradient radius 628 | float gradRadius = MIN(self.bounds.size.width , self.bounds.size.height) ; 629 | //Gradient draw 630 | CGContextDrawRadialGradient (context, gradient, gradCenter, 631 | 0, gradCenter, gradRadius, 632 | kCGGradientDrawsAfterEndLocation); 633 | CGGradientRelease(gradient); 634 | } 635 | 636 | // Set background rect color 637 | if (self.color) { 638 | CGContextSetFillColorWithColor(context, self.color.CGColor); 639 | } else { 640 | CGContextSetGrayFillColor(context, 0.0f, self.opacity); 641 | } 642 | 643 | 644 | // Center HUD 645 | CGRect allRect = self.bounds; 646 | // Draw rounded HUD backgroud rect 647 | CGRect boxRect = CGRectMake(round((allRect.size.width - size.width) / 2) + self.xOffset, 648 | round((allRect.size.height - size.height) / 2) + self.yOffset, size.width, size.height); 649 | float radius = self.cornerRadius; 650 | CGContextBeginPath(context); 651 | CGContextMoveToPoint(context, CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect)); 652 | CGContextAddArc(context, CGRectGetMaxX(boxRect) - radius, CGRectGetMinY(boxRect) + radius, radius, 3 * (float)M_PI / 2, 0, 0); 653 | CGContextAddArc(context, CGRectGetMaxX(boxRect) - radius, CGRectGetMaxY(boxRect) - radius, radius, 0, (float)M_PI / 2, 0); 654 | CGContextAddArc(context, CGRectGetMinX(boxRect) + radius, CGRectGetMaxY(boxRect) - radius, radius, (float)M_PI / 2, (float)M_PI, 0); 655 | CGContextAddArc(context, CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect) + radius, radius, (float)M_PI, 3 * (float)M_PI / 2, 0); 656 | CGContextClosePath(context); 657 | CGContextFillPath(context); 658 | 659 | UIGraphicsPopContext(); 660 | } 661 | 662 | #pragma mark - KVO 663 | 664 | - (void)registerForKVO { 665 | for (NSString *keyPath in [self observableKeypaths]) { 666 | [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:NULL]; 667 | } 668 | } 669 | 670 | - (void)unregisterFromKVO { 671 | for (NSString *keyPath in [self observableKeypaths]) { 672 | [self removeObserver:self forKeyPath:keyPath]; 673 | } 674 | } 675 | 676 | - (NSArray *)observableKeypaths { 677 | return [NSArray arrayWithObjects:@"mode", @"customView", @"labelText", @"labelFont", @"labelColor", 678 | @"detailsLabelText", @"detailsLabelFont", @"detailsLabelColor", @"progress", @"activityIndicatorColor", nil]; 679 | } 680 | 681 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 682 | if (![NSThread isMainThread]) { 683 | [self performSelectorOnMainThread:@selector(updateUIForKeypath:) withObject:keyPath waitUntilDone:NO]; 684 | } else { 685 | [self updateUIForKeypath:keyPath]; 686 | } 687 | } 688 | 689 | - (void)updateUIForKeypath:(NSString *)keyPath { 690 | if ([keyPath isEqualToString:@"mode"] || [keyPath isEqualToString:@"customView"] || 691 | [keyPath isEqualToString:@"activityIndicatorColor"]) { 692 | [self updateIndicators]; 693 | } else if ([keyPath isEqualToString:@"labelText"]) { 694 | label.text = self.labelText; 695 | } else if ([keyPath isEqualToString:@"labelFont"]) { 696 | label.font = self.labelFont; 697 | } else if ([keyPath isEqualToString:@"labelColor"]) { 698 | label.textColor = self.labelColor; 699 | } else if ([keyPath isEqualToString:@"detailsLabelText"]) { 700 | detailsLabel.text = self.detailsLabelText; 701 | } else if ([keyPath isEqualToString:@"detailsLabelFont"]) { 702 | detailsLabel.font = self.detailsLabelFont; 703 | } else if ([keyPath isEqualToString:@"detailsLabelColor"]) { 704 | detailsLabel.textColor = self.detailsLabelColor; 705 | } else if ([keyPath isEqualToString:@"progress"]) { 706 | if ([indicator respondsToSelector:@selector(setProgress:)]) { 707 | [(id)indicator setValue:@(progress) forKey:@"progress"]; 708 | } 709 | return; 710 | } 711 | [self setNeedsLayout]; 712 | [self setNeedsDisplay]; 713 | } 714 | 715 | #pragma mark - Notifications 716 | 717 | - (void)registerForNotifications { 718 | NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 719 | 720 | [nc addObserver:self selector:@selector(statusBarOrientationDidChange:) 721 | name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; 722 | } 723 | 724 | - (void)unregisterFromNotifications { 725 | NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 726 | [nc removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; 727 | } 728 | 729 | - (void)statusBarOrientationDidChange:(NSNotification *)notification { 730 | UIView *superview = self.superview; 731 | if (!superview) { 732 | return; 733 | } else { 734 | [self updateForCurrentOrientationAnimated:YES]; 735 | } 736 | } 737 | 738 | - (void)updateForCurrentOrientationAnimated:(BOOL)animated { 739 | // Stay in sync with the superview in any case 740 | if (self.superview) { 741 | self.bounds = self.superview.bounds; 742 | [self setNeedsDisplay]; 743 | } 744 | 745 | // Not needed on iOS 8+, compile out when the deployment target allows, 746 | // to avoid sharedApplication problems on extension targets 747 | #if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000 748 | // Only needed pre iOS 7 when added to a window 749 | BOOL iOS8OrLater = kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_8_0; 750 | if (iOS8OrLater || ![self.superview isKindOfClass:[UIWindow class]]) return; 751 | 752 | UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 753 | CGFloat radians = 0; 754 | if (UIInterfaceOrientationIsLandscape(orientation)) { 755 | if (orientation == UIInterfaceOrientationLandscapeLeft) { radians = -(CGFloat)M_PI_2; } 756 | else { radians = (CGFloat)M_PI_2; } 757 | // Window coordinates differ! 758 | self.bounds = CGRectMake(0, 0, self.bounds.size.height, self.bounds.size.width); 759 | } else { 760 | if (orientation == UIInterfaceOrientationPortraitUpsideDown) { radians = (CGFloat)M_PI; } 761 | else { radians = 0; } 762 | } 763 | rotationTransform = CGAffineTransformMakeRotation(radians); 764 | 765 | if (animated) { 766 | [UIView beginAnimations:nil context:nil]; 767 | [UIView setAnimationDuration:0.3]; 768 | } 769 | [self setTransform:rotationTransform]; 770 | if (animated) { 771 | [UIView commitAnimations]; 772 | } 773 | #endif 774 | } 775 | 776 | @end 777 | 778 | 779 | @implementation MBRoundProgressView 780 | 781 | #pragma mark - Lifecycle 782 | 783 | - (id)init { 784 | return [self initWithFrame:CGRectMake(0.f, 0.f, 37.f, 37.f)]; 785 | } 786 | 787 | - (id)initWithFrame:(CGRect)frame { 788 | self = [super initWithFrame:frame]; 789 | if (self) { 790 | self.backgroundColor = [UIColor clearColor]; 791 | self.opaque = NO; 792 | _progress = 0.f; 793 | _annular = NO; 794 | _progressTintColor = [[UIColor alloc] initWithWhite:1.f alpha:1.f]; 795 | _backgroundTintColor = [[UIColor alloc] initWithWhite:1.f alpha:.1f]; 796 | [self registerForKVO]; 797 | } 798 | return self; 799 | } 800 | 801 | - (void)dealloc { 802 | [self unregisterFromKVO]; 803 | #if !__has_feature(objc_arc) 804 | [_progressTintColor release]; 805 | [_backgroundTintColor release]; 806 | [super dealloc]; 807 | #endif 808 | } 809 | 810 | #pragma mark - Drawing 811 | 812 | - (void)drawRect:(CGRect)rect { 813 | 814 | CGRect allRect = self.bounds; 815 | CGRect circleRect = CGRectInset(allRect, 2.0f, 2.0f); 816 | CGContextRef context = UIGraphicsGetCurrentContext(); 817 | 818 | if (_annular) { 819 | // Draw background 820 | BOOL isPreiOS7 = kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_7_0; 821 | CGFloat lineWidth = isPreiOS7 ? 5.f : 2.f; 822 | UIBezierPath *processBackgroundPath = [UIBezierPath bezierPath]; 823 | processBackgroundPath.lineWidth = lineWidth; 824 | processBackgroundPath.lineCapStyle = kCGLineCapButt; 825 | CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2); 826 | CGFloat radius = (self.bounds.size.width - lineWidth)/2; 827 | CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees 828 | CGFloat endAngle = (2 * (float)M_PI) + startAngle; 829 | [processBackgroundPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES]; 830 | [_backgroundTintColor set]; 831 | [processBackgroundPath stroke]; 832 | // Draw progress 833 | UIBezierPath *processPath = [UIBezierPath bezierPath]; 834 | processPath.lineCapStyle = isPreiOS7 ? kCGLineCapRound : kCGLineCapSquare; 835 | processPath.lineWidth = lineWidth; 836 | endAngle = (self.progress * 2 * (float)M_PI) + startAngle; 837 | [processPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES]; 838 | [_progressTintColor set]; 839 | [processPath stroke]; 840 | } else { 841 | // Draw background 842 | [_progressTintColor setStroke]; 843 | [_backgroundTintColor setFill]; 844 | CGContextSetLineWidth(context, 2.0f); 845 | CGContextFillEllipseInRect(context, circleRect); 846 | CGContextStrokeEllipseInRect(context, circleRect); 847 | // Draw progress 848 | CGPoint center = CGPointMake(allRect.size.width / 2, allRect.size.height / 2); 849 | CGFloat radius = (allRect.size.width - 4) / 2; 850 | CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees 851 | CGFloat endAngle = (self.progress * 2 * (float)M_PI) + startAngle; 852 | [_progressTintColor setFill]; 853 | CGContextMoveToPoint(context, center.x, center.y); 854 | CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0); 855 | CGContextClosePath(context); 856 | CGContextFillPath(context); 857 | } 858 | } 859 | 860 | #pragma mark - KVO 861 | 862 | - (void)registerForKVO { 863 | for (NSString *keyPath in [self observableKeypaths]) { 864 | [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:NULL]; 865 | } 866 | } 867 | 868 | - (void)unregisterFromKVO { 869 | for (NSString *keyPath in [self observableKeypaths]) { 870 | [self removeObserver:self forKeyPath:keyPath]; 871 | } 872 | } 873 | 874 | - (NSArray *)observableKeypaths { 875 | return [NSArray arrayWithObjects:@"progressTintColor", @"backgroundTintColor", @"progress", @"annular", nil]; 876 | } 877 | 878 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 879 | [self setNeedsDisplay]; 880 | } 881 | 882 | @end 883 | 884 | 885 | @implementation MBBarProgressView 886 | 887 | #pragma mark - Lifecycle 888 | 889 | - (id)init { 890 | return [self initWithFrame:CGRectMake(.0f, .0f, 120.0f, 20.0f)]; 891 | } 892 | 893 | - (id)initWithFrame:(CGRect)frame { 894 | self = [super initWithFrame:frame]; 895 | if (self) { 896 | _progress = 0.f; 897 | _lineColor = [UIColor whiteColor]; 898 | _progressColor = [UIColor whiteColor]; 899 | _progressRemainingColor = [UIColor clearColor]; 900 | self.backgroundColor = [UIColor clearColor]; 901 | self.opaque = NO; 902 | [self registerForKVO]; 903 | } 904 | return self; 905 | } 906 | 907 | - (void)dealloc { 908 | [self unregisterFromKVO]; 909 | #if !__has_feature(objc_arc) 910 | [_lineColor release]; 911 | [_progressColor release]; 912 | [_progressRemainingColor release]; 913 | [super dealloc]; 914 | #endif 915 | } 916 | 917 | #pragma mark - Drawing 918 | 919 | - (void)drawRect:(CGRect)rect { 920 | CGContextRef context = UIGraphicsGetCurrentContext(); 921 | 922 | CGContextSetLineWidth(context, 2); 923 | CGContextSetStrokeColorWithColor(context,[_lineColor CGColor]); 924 | CGContextSetFillColorWithColor(context, [_progressRemainingColor CGColor]); 925 | 926 | // Draw background 927 | float radius = (rect.size.height / 2) - 2; 928 | CGContextMoveToPoint(context, 2, rect.size.height/2); 929 | CGContextAddArcToPoint(context, 2, 2, radius + 2, 2, radius); 930 | CGContextAddLineToPoint(context, rect.size.width - radius - 2, 2); 931 | CGContextAddArcToPoint(context, rect.size.width - 2, 2, rect.size.width - 2, rect.size.height / 2, radius); 932 | CGContextAddArcToPoint(context, rect.size.width - 2, rect.size.height - 2, rect.size.width - radius - 2, rect.size.height - 2, radius); 933 | CGContextAddLineToPoint(context, radius + 2, rect.size.height - 2); 934 | CGContextAddArcToPoint(context, 2, rect.size.height - 2, 2, rect.size.height/2, radius); 935 | CGContextFillPath(context); 936 | 937 | // Draw border 938 | CGContextMoveToPoint(context, 2, rect.size.height/2); 939 | CGContextAddArcToPoint(context, 2, 2, radius + 2, 2, radius); 940 | CGContextAddLineToPoint(context, rect.size.width - radius - 2, 2); 941 | CGContextAddArcToPoint(context, rect.size.width - 2, 2, rect.size.width - 2, rect.size.height / 2, radius); 942 | CGContextAddArcToPoint(context, rect.size.width - 2, rect.size.height - 2, rect.size.width - radius - 2, rect.size.height - 2, radius); 943 | CGContextAddLineToPoint(context, radius + 2, rect.size.height - 2); 944 | CGContextAddArcToPoint(context, 2, rect.size.height - 2, 2, rect.size.height/2, radius); 945 | CGContextStrokePath(context); 946 | 947 | CGContextSetFillColorWithColor(context, [_progressColor CGColor]); 948 | radius = radius - 2; 949 | float amount = self.progress * rect.size.width; 950 | 951 | // Progress in the middle area 952 | if (amount >= radius + 4 && amount <= (rect.size.width - radius - 4)) { 953 | CGContextMoveToPoint(context, 4, rect.size.height/2); 954 | CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius); 955 | CGContextAddLineToPoint(context, amount, 4); 956 | CGContextAddLineToPoint(context, amount, radius + 4); 957 | 958 | CGContextMoveToPoint(context, 4, rect.size.height/2); 959 | CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius); 960 | CGContextAddLineToPoint(context, amount, rect.size.height - 4); 961 | CGContextAddLineToPoint(context, amount, radius + 4); 962 | 963 | CGContextFillPath(context); 964 | } 965 | 966 | // Progress in the right arc 967 | else if (amount > radius + 4) { 968 | float x = amount - (rect.size.width - radius - 4); 969 | 970 | CGContextMoveToPoint(context, 4, rect.size.height/2); 971 | CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius); 972 | CGContextAddLineToPoint(context, rect.size.width - radius - 4, 4); 973 | float angle = -acos(x/radius); 974 | if (isnan(angle)) angle = 0; 975 | CGContextAddArc(context, rect.size.width - radius - 4, rect.size.height/2, radius, M_PI, angle, 0); 976 | CGContextAddLineToPoint(context, amount, rect.size.height/2); 977 | 978 | CGContextMoveToPoint(context, 4, rect.size.height/2); 979 | CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius); 980 | CGContextAddLineToPoint(context, rect.size.width - radius - 4, rect.size.height - 4); 981 | angle = acos(x/radius); 982 | if (isnan(angle)) angle = 0; 983 | CGContextAddArc(context, rect.size.width - radius - 4, rect.size.height/2, radius, -M_PI, angle, 1); 984 | CGContextAddLineToPoint(context, amount, rect.size.height/2); 985 | 986 | CGContextFillPath(context); 987 | } 988 | 989 | // Progress is in the left arc 990 | else if (amount < radius + 4 && amount > 0) { 991 | CGContextMoveToPoint(context, 4, rect.size.height/2); 992 | CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius); 993 | CGContextAddLineToPoint(context, radius + 4, rect.size.height/2); 994 | 995 | CGContextMoveToPoint(context, 4, rect.size.height/2); 996 | CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius); 997 | CGContextAddLineToPoint(context, radius + 4, rect.size.height/2); 998 | 999 | CGContextFillPath(context); 1000 | } 1001 | } 1002 | 1003 | #pragma mark - KVO 1004 | 1005 | - (void)registerForKVO { 1006 | for (NSString *keyPath in [self observableKeypaths]) { 1007 | [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:NULL]; 1008 | } 1009 | } 1010 | 1011 | - (void)unregisterFromKVO { 1012 | for (NSString *keyPath in [self observableKeypaths]) { 1013 | [self removeObserver:self forKeyPath:keyPath]; 1014 | } 1015 | } 1016 | 1017 | - (NSArray *)observableKeypaths { 1018 | return [NSArray arrayWithObjects:@"lineColor", @"progressRemainingColor", @"progressColor", @"progress", nil]; 1019 | } 1020 | 1021 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 1022 | [self setNeedsDisplay]; 1023 | } 1024 | 1025 | @end 1026 | --------------------------------------------------------------------------------