In
Folio Case, I'm using a custom UISlider with annotations to allow users to easily skim through the document and find the page they want. When the user taps the knob, a small bubble shows up that shows the page number that corresponds to the position where the knob currently is. The page number within the bubble is constantly updated as the user drags the knob around. When the user lets go, the app turns to the selected page, the knob snaps in place and the bubble slowly fades out. This approach makes sure that the user gets the information when he/she needs it and it doesn't stand in the way of getting things done.
Ever since
Folio Case was released, I've been asked how I achieved this effect, as there is no built-in functionality for this behaviour in UIKit. The short answer is: using KVO (key value observing), I track whether the knob is being touched and moved and react appropriately.
The long answer is, well, longer. If you're not familiar with the concept of KVO, you might want to read Apple's
Key Value Observing Programming Guide. In short, Apple describes KVO as "a mechanism that allows objects to be notified of changes to specific properties of other objects". In practical use, this means that I can register as an observer to any property other classes expose, such as a UIView's frame. If I choose to do so, I will be notified whenever the object is being moved within its superview, which causes its frame to change.
Now, in order to track a UISlider's knob movement, we will have to get a little tricky. A UISlider instance, once it is being displayed on the screen, has three subviews, all of which are instances of UIImageView. The third and foremost UIImageView instance contains the UISlider's knob. This might change at any time, so if you're going to use an approach like this in production code, be aware that it might change with the next release of iOS. However, the worst thing that can happen in this case is that the bubble won't be shown anymore.
In
ARAnnotatedSliderView, which is my class to manage the UISlider and the bubble that is being conditionally displayed, I have the following method:
kARPageScrubberViewKeypathFrame and
kARPageScrubberViewKeypathTracking are simple constant NSStrings,
@"frame" and
@"tracking", respectively. The first property I want to observe is the knob's frame, to be notified of changes in the knob's positioning. The second property I want to observe is the UISlider's
tracking property, to be notified when the user starts tracking the knob with his/her finger and when they stop, so I can dismiss the bubble. When one of those notifications kicks in, I react to it using this method:
When the knob's frame was changed, this code makes sure that the bubble view stays right on top of it to ensure a smooth experience for the user. When tracking starts, I can use this notification to display the bubble, and when tracking stops, I can set up a timer to dismiss the bubble after a short while.
As you can see from the complete code, this class is taken from
Folio Case without (many) changes, and so is not well configurable. The bubble will always display "Page %d" where a class that was intended as being reusable would probably allow you to customize this setting. I've set up a
small example project for this on Github, if you'd like to check it out and play with it. If someone ends up making a fully reusable class out of this, please let me know.
If you'd like to get back to me, feel free to leave a comment here or
follow me on Twitter.
Latest Comments
Thu, 05.05.2011 21:12
Very kind of you to share this , Alexander. A couple of minor corrections, if I may: In your addObservers:... me [...]
Mon, 02.05.2011 01:15
blog.proculo.de site is cool . Bookmarked !!! [url=http ://betonufc.info]ufc betting[/ url]
Tue, 05.04.2011 21:10
You may find it interesting to note that I have now decided to return the Android phone I bought and go back to an [...]
Tue, 05.04.2011 18:10
I have an Android device and f or me, it's not the UX of the OS that's the problem. Thanks to the dedicated keys, a [...]
Tue, 05.04.2011 17:41
Matthew, note that I never said Mark shouldn't have switc hed to an Android phone. If th at is his choice, and he [...]