Note: this is an updated post based on an older one, due to renaming the project (was "DSActivityView") and moving it from Subversion to GitHub.
I wrote a reusable class for a couple of iOS apps I was working on, called
DejalActivityView. I decided to release it as open source. It has proven quite popular with others, too. Read on for details (including a demo movie).
Firstly, I should say that this work was inspired in part by Matt Gallagher's excellent article, Showing a "Loading..." message over the iPhone keyboard. My code only uses the
-keyboardView method from his article, but he deserves credit and thanks for that and many other helpful articles. If you're not reading his blog, Cocoa with Love, you're doing yourself a disservice.
Back to my class. Actually, there are four classes:
DejalKeyboardActivityView. They provide four styles of activity view, and could easily be extended to support more.
This does a simple horizontal-style loading view, intended for situations where you have a blank view while loading data. It can be displayed very easily — for the default "Loading..." label text, simply use:
The activity view is automatically added as a subview of the specified view (e.g. the current content view). No need to save the result to an ivar. It automatically supports rotation to any orientation, too.
You can specify a custom label via:
Or specify a custom width, e.g. so you can change the label while it is being displayed without upsetting the geometry, via:
You can also have it manage the network activity indicator in the status bar, via a
showNetworkActivityIndicator boolean property. It is
NO by default, but if set to
YES the network activity indicator in the status bar will be displayed, and automatically hidden when the
DejalActivityView is removed. For example:
You can also toggle it on an already-visible activity view:
When you're done with the activity view, simply invoke this to get rid of it:
This is the same as
DejalActivityView, but with a white indicator and text instead of black, for use in dark views.
This is a subclass of
DejalActivityView, which displays an animated round-rect-enclosed variation: it animates into view by zooming from full-screen, with a gray background fading in to cover the passed view, and animates out by zooming to half size and fading out the background (see below for a movie showing it in action). It is ideal for situations where you have content visible already, but want to do a network operation to validate or send data, or some other time-consuming activity.
Display it the same way as
[DejalBezelActivityView activityViewForView:withLabel:] and
[DejalBezelActivityView activityViewForView:withLabel:width:] variations are also available.
You can also split the label over multiple lines, thanks to a change contributed by Suleman Sidat. Thank you! Simply include one or more
\n sequences in the label text, e.g.
Similarly, to display an activity view with just the activity indicator, and no label, simply specify a blank label:
To remove with animation, call:
This is a subclass of
DejalBezelActivityView, which displays over the keyboard, somewhat like the iOS 2 Text app used to do. It is useful to simply prevent further typing while validating a field or sending data (though you might also want to disable the field, to prevent pasteboard operations on it). No need to specify a view to use for this, since it uses the keyboard. (This class may be removed in the future, since it isn't that useful nowadays; let me know if you need it.)
[DejalKeyboardActivityView activityViewWithLabel:] variation for custom text. Remove it the same as for the bezel style:
I've included a demo project that builds an app to show the various options: the four styles, default or custom label text, covering just the content view or whole window, etc. It requires iOS 5. Here's a movie of an earlier version of the demo, showing the demo app running (this project used to be called "DSActivityView"):
You can get the code and more information from the Dejal Open Source page.