IPhone Tech Talks/Advanced UIKit and Device Features
From ceejayoz
Matt Drance (mdrance@apple.com)
- Animation, text input, interruptions, URL communication
- Address book, movie player, camera and photo library
Contents |
[edit] Animation
- everywhere on iphone, each UIView has a Core Animation layer
- size and pos, opacity, and transitions
[edit] Animation blocks
[UIView beginAnimations:@"scaleDown" context:nil]; [UIView setAnimationDuration:1.0]; // view changes are done here [UIView commitAnimations];
[edit] Text input/output
- output is easy, use UILabel, UIWebView, NSString (UIStringDrawing)
- input easy too! use UITextField, UITextView
[edit] Keyboard
- customize in Interface Builder or UITextInputTraits
- shown programmatically with becomeFirstResponder (if you, say, wanted it to show up on viewDidAppear)
- hide with resignFirstResponder call (focus/blur from JavaScript, essentially)
- UIWindow notifications
- UIKeyboardWillShowNotification
- UIKeyboardWillHIdeNotification
- they include keyboard dimensions, use this to place stuff right next to keyboard etc.
- powerful delegate model
- autocorrection occurs very late - wait for textFieldDidEndEditing
- have an exit strategy
- cancel/done button
- implement textFieldShouldReturn
- built in UISearchBar support - showsCancelButton = YES
[edit] Handling interruptions
- can come at any time
- phone call, SMS, Calendar alert
- applicationWillResignActive:
- preserve application state, pause audio/graphics/animations
- if user dismisses, applicationDidResumeActive: fires
- if user accepts interruption, applicationWillTerminate: fires (like home button press) - potential data loss! deal with this!!!
[edit] URL Communication
Opening External URLs:
NSURL *url = [NSURL URLWithString:urlString]; [[UIApplication sharedApplication] openURL:url];
- will quit when called, as another app will handle the URL
- http, https, feed - Safari
- mailto - Mail
- Google Maps
- YouTube
- Phobos.apple.com for appstore/itunes
Can declare your own URL types
- done via Info.plist - CFBundleURLTypes array
Receive URLs via application delegate - handleOpenURL, then get and parse the query string
- secure your URL handlers! don't provide an API for everything under the sun!
[edit] Address Book
- full read/write access!
- reusable view controllers too
- ABPeoplePickerNavigationController
- ABPersonViewController
- ABNewPersonViewController
- ABUnknownPersonViewController
[edit] Presentation
- present modally, as is a navigation controller - will have two nav bars if you push on top of existing
- add person controllers to a navigation controller
[edit] Movie Player
- H.264 Baseline @ 640x480 max
- always in landscape orientation
- start/stop with loaded/finished notifications
- static only, no streaming via RTSP :-(
[edit] Camera and Photo Library
- UIImagePickerController
- access camera and photo library
- check for camera via isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera (iPod touch doesn't have one)
- present modally ([self presentModalViewController:picker animated:YES];)
- didFinishPickingImage with editingInfo (original image, rectangle that was selected via crop/zoom)
- see Photo Tagger app! useful!
- load images on demand
- use imageWithContentsOfFile: instead of imageNamed: - allows direct control of image memory allocation (imageNamed system caches it)
- use PNGs, if at all possible
- scale to optimal size using drawInRect: to reduce memory load (camera images are 1600x1200!)
[edit] Saving images
- can save any image to the photos album
- UIImageWriteToSavedPhotosAlbum
- adds to Camera Roll on iPhone, Saved Photos on iPod touch
- can save locally, too, for caching etc. in sandbox
- can use renderInContext:UIGraphicsGetCurrentContext() to take screenshot of a view
[edit] Multi-touch
- one of the few cases you'll subclass UIView
[edit] single-touch
- implement touchesBegan:withEvent: (get array with all UITouch sources)
- implement touchesMoved:withEvent: (fires repeatedly on moving)
- implement touchesEnded:withEvent: (when lift finger)
[edit] single-tap / double-tap
- UITouch has a Tap Count (1/2)
[edit] multiple-touch
- same implementation, just more information
- second finger calls touchesBegan again, now get two UITouch objects (if one finger isn't moving, phase is 'Stationary')
- see Touches sample code
[edit] detecting a swipe
- get starting point in touchesBegan
- track motion in touchesMoved
- straightish?
- dragged far enough?
- dragged quickly enough?
[edit] Accelerometer
[edit] three-axis data
- x-axis is counterintuitive, left is positive
- use for interface orientation, device orientation, analog user input (games!)
[edit] analog data
- 10-100 hertz approx range
- 30-60 for games, generally
- callbacks should be lean, so queuing doesn't happen
- use filters to remove noise (see BubbleLevel sample)
[edit] Core Location
[edit] Location Sources
- cell triangulation
- wireless access points
- GPS
- single API
- CILocationManager
- set up distanceFilter and desiredAccuracy ('let me know when I've moved 3km')
- location updates give previous and current location
- check timestamps to avoid cached data (think Twinkle thinking you're still in Rochester when you're in Buffalo)
- new in 2.2 - direction and speed updates
[edit] Privacy
- location prompt on first use of startUpdatingLocation (and every other use unless say OK)
- plan for kCLErrorDenied
- locationManager:didFailWithError:
- kCLErrorDenied - user said don't allow, stop asking until next launch?
- kCLErrorLocationUnknown - often temporary, keeps trying until stopUpdatingLocation
[edit] Battery life
- use a 30 second timeout
- try to use minimum accuracy possible - saves battery
- stop updating and release when finished

