41
TANSTAAFL: using open source iPhone UI code Jonathan Saggau (@jonmarimba) Sounds Broken inc

303 TANSTAAFL: Using Open Source iPhone UI Code

Embed Size (px)

DESCRIPTION

303 TANSTAAFL: Using Open Source iPhone UI Code Tuesday, Sept. 28, 2:00 — 3:15 pm

Citation preview

Page 1: 303 TANSTAAFL: Using Open Source iPhone UI Code

TANSTAAFL: using open source iPhone UI

codeJonathan Saggau (@jonmarimba)

Sounds Broken inc

Page 2: 303 TANSTAAFL: Using Open Source iPhone UI Code

Da book

Page 3: 303 TANSTAAFL: Using Open Source iPhone UI Code

The dude

Page 4: 303 TANSTAAFL: Using Open Source iPhone UI Code

Three20 image viewer

Apple Joe++

Page 5: 303 TANSTAAFL: Using Open Source iPhone UI Code

Three20 image viewer

Apple Joe++

Page 6: 303 TANSTAAFL: Using Open Source iPhone UI Code

Demoor... Julia Child impression

Page 7: 303 TANSTAAFL: Using Open Source iPhone UI Code

How to use three20 photo views

Static Library; pay special attention to linker flags on web site.

<TTPhotoSource> conforming object, which vends: <TTPhoto> conforming objects

Note: <TTModel, TTURLObject>

Um. What?

Page 8: 303 TANSTAAFL: Using Open Source iPhone UI Code

/** * TTModel describes the state of an object that can be loaded from a remote source. * * By implementing this protocol, you can communicate to the user the state of network * activity in an object. */@protocol TTModel <NSObject>

/** * An array of objects that conform to the TTModelDelegate protocol. */- (NSMutableArray*)delegates;

/** * Indicates that the data has been loaded. * * Default implementation returns YES. */- (BOOL)isLoaded;

Page 9: 303 TANSTAAFL: Using Open Source iPhone UI Code

/** * Indicates that the data is in the process of loading. * * Default implementation returns NO. */- (BOOL)isLoading;

/** * Indicates that the data is in the process of loading additional data. * * Default implementation returns NO. */- (BOOL)isLoadingMore;

/** * Indicates that the model is of date and should be reloaded as soon as possible. * * Default implementation returns NO. */-(BOOL)isOutdated;

Page 10: 303 TANSTAAFL: Using Open Source iPhone UI Code

/** * Loads the model. * * Default implementation does nothing. */- (void)load:(TTURLRequestCachePolicy)cachePolicy more:(BOOL)more;

/** * Cancels a load that is in progress. * * Default implementation does nothing. */- (void)cancel;

/** * Invalidates data stored in the cache or optionally erases it. * * Default implementation does nothing. */- (void)invalidate:(BOOL)erase;

@end

Page 11: 303 TANSTAAFL: Using Open Source iPhone UI Code

@protocol TTURLObject <NSObject>@optional

/** * Converts the object to a URL using TTURLMap. */@property (nonatomic, readonly) NSString* URLValue;

/** * Converts the object to a specially-named URL using TTURLMap. */- (NSString*)URLValueWithName:(NSString*)name;

@end

Page 12: 303 TANSTAAFL: Using Open Source iPhone UI Code

How it works

TTPhotoViewController and TTThumbsViewController

TTURLCache caches images to memory and / or disk

TTURLRequestQueue handles downloading images

Page 13: 303 TANSTAAFL: Using Open Source iPhone UI Code

@protocol TTPhotoSource <TTModel, TTURLObject>

/** * The title of this collection of photos. */@property (nonatomic, copy) NSString* title;

/** * The total number of photos in the source, independent of the number that have been loaded. */@property (nonatomic, readonly) NSInteger numberOfPhotos;

/** * The maximum index of photos that have already been loaded. */@property (nonatomic, readonly) NSInteger maxPhotoIndex;

/** * */- (id<TTPhoto>)photoAtIndex:(NSInteger)index;

@end

Page 14: 303 TANSTAAFL: Using Open Source iPhone UI Code

@protocol TTPhoto <NSObject, TTURLObject>

/** * The photo source that the photo belongs to. */@property (nonatomic, assign) id<TTPhotoSource> photoSource;

/** * The index of the photo within its photo source. */@property (nonatomic) CGSize size;

/** * The index of the photo within its photo source. */@property (nonatomic) NSInteger index;

/** * The caption of the photo. */@property (nonatomic, copy) NSString* caption;

/** * Gets the URL of one of the differently sized versions of the photo. */- (NSString*)URLForVersion:(TTPhotoVersion)version;

@end

Page 15: 303 TANSTAAFL: Using Open Source iPhone UI Code

can use bundle://someImageFileNameHere as URL string to load images from app bundle

can use documents://someImageFileNameHere as URL string to load images from user documents folder

Page 16: 303 TANSTAAFL: Using Open Source iPhone UI Code

Other cool stuff about three20

URL-based navigation that can persist the path taken to get to a given view controller and can push that stack of

views back on when the user re-opens your app.

Stylesheets to “skin” your app easily.

Pretty buttons and overlays.

Decent (visual) error handling for cloud-based apps

Simpler UITableView programming using TTModel*

Page 17: 303 TANSTAAFL: Using Open Source iPhone UI Code

Documentation and help

Mailing list: http://groups.google.com/group/three20

API docs: http://three20.info/

Me! [email protected] @jonmarimba

(I’m not affiliated with three20, I just use it a lot)

Page 18: 303 TANSTAAFL: Using Open Source iPhone UI Code

Calendar view with Kal

Apple Keith

Page 19: 303 TANSTAAFL: Using Open Source iPhone UI Code
Page 20: 303 TANSTAAFL: Using Open Source iPhone UI Code

@protocol KalDataSourceCallbacks;

@protocol KalDataSource <NSObject, UITableViewDataSource>- (void)presentingDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate delegate: (id<KalDataSourceCallbacks>)delegate;- (NSArray *)markedDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate;- (void)loadItemsFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate;- (void)removeAllItems;@end

@protocol KalDataSourceCallbacks <NSObject>- (void)loadedDataSource:(id<KalDataSource>)dataSource;@end

Page 21: 303 TANSTAAFL: Using Open Source iPhone UI Code

Coverflow view with OpenFlow

Apple Alex

Page 22: 303 TANSTAAFL: Using Open Source iPhone UI Code

Demoor... Julia Child impression

Page 23: 303 TANSTAAFL: Using Open Source iPhone UI Code

How to use it.@protocol AFOpenFlowViewDataSource <NSObject>

//Tells the data source that will need this image. //The data source should set the image as soon as it’s ready- (void)openFlowView:(AFOpenFlowView *)openFlowView requestImageForIndex:(int)index;- (UIImage *)defaultImage;

@end

//Allows the delegate to update other parts of UI for selection@protocol AFOpenFlowViewDelegate <NSObject>@optional- (void)openFlowView:(AFOpenFlowView *)openFlowView selectionDidChange:(int)index;@end

Page 24: 303 TANSTAAFL: Using Open Source iPhone UI Code

How to use it.

[(AFOpenFlowView *)someView setImage:someImage forIndex:someInteger];

BTW: The sample code also has a useful image loading subclass of NSOperation.

If you want to change the image for a given index, do this:

Page 25: 303 TANSTAAFL: Using Open Source iPhone UI Code

How it works

// Set some perspective CATransform3D sublayerTransform = CATransform3DIdentity; sublayerTransform.m34 = -0.01; // 1 / -zDistance where zDistance = 100 [scrollView.layer setSublayerTransform:sublayerTransform];

CoreAnimation 2.5D++

Dequeues images much like the UITableView (and the TTPhotoview controller, too)

Draws only those images that are on screen

// the magic is here- (void)layoutCover:(AFItemView *)aCover selectedCover:(int)selectedIndex animated:(Boolean)animated

Page 26: 303 TANSTAAFL: Using Open Source iPhone UI Code

Beware The Uncanny Valley.

Wikipedia User:Smurrayinchester (Creative Commons Attribution ShareAlike 3.0)

Page 27: 303 TANSTAAFL: Using Open Source iPhone UI Code

Coverflow view with OpenFlow

Apple Alex

Page 28: 303 TANSTAAFL: Using Open Source iPhone UI Code

What needs a-changin’The animation is a little “off” for the rotation (seems like

it’s the wrong speed)

The z position of the flanking photos is not far enough away.

Feels like there is a lot of friction.

The reflection under the covers is semi-transparent. Apple’s view has the reflected images occlude those

behind as in “real life.”

Page 29: 303 TANSTAAFL: Using Open Source iPhone UI Code

How I “fixed” it.

Let a scroll view do the animating left to right and set which page is selected (and therefore rotated to face the

user) based on screen centerline.

Hacking others’ code is fun (really)

// UIScrollViewDecelerationRateNormal = 0.998// UIScrollViewDecelerationRateFast = 0.990self.decelerationRate = .992;

Page 30: 303 TANSTAAFL: Using Open Source iPhone UI Code

How I “fixed” it.scrolling a UIScrollView sets bounds constantly

and calls layoutSubviews a lot...- (void)layoutSubviews{ NSLog(@"[%@ %s]", self, _cmd); halfScreenWidth = self.bounds.size.width / 2; halfScreenHeight = self.bounds.size.height / 2; int lowerBound = MAX(-1, selectedCoverView.number - COVER_BUFFER); int upperBound = MIN(self.numberOfImages - 1, selectedCoverView.number + COVER_BUFFER); [self layoutCovers:selectedCoverView.number fromCover:lowerBound toCover:upperBound]; [self setNumberOfImages:numberOfImages]; // resets view bounds and stuff CGPoint contentOffset = [self contentOffset]; int targetCover = (int) roundf(contentOffset.x / COVER_SPACING); if (targetCover != selectedCoverView.number) { if (targetCover < 0) [self setSelectedCover:0]; else if (targetCover >= self.numberOfImages) [self setSelectedCover:self.numberOfImages - 1]; else [self setSelectedCover:targetCover]; }}

// 1 / -zDistance where zDistance = 100

Page 31: 303 TANSTAAFL: Using Open Source iPhone UI Code

- (SBNotifyingWindow *)appWindow{ id appDel = [[UIApplication sharedApplication] delegate]; if([appDel respondsToSelector:@selector(window)]) { UIWindow *window = [appDel performSelector:@selector(window)]; if([window isMemberOfClass:[SBNotifyingWindow class]]) { return (SBNotifyingWindow *)window; } } return nil;}

- (void)setUpInitialState { [[self appWindow] addObjectInterestedInTouches:self];

Hijacking touch eventsI think I stole this from Apple’s sample code

Page 32: 303 TANSTAAFL: Using Open Source iPhone UI Code

#pragma mark SBNotifyingWindowTouches

-(void)interestingEvent:(UIEvent *)event;{ //NSLog(@"%@ %s %@", self, _cmd, event); NSSet *touches = [event allTouches]; UITouch *touch = [touches anyObject]; UITouchPhase phase = [touch phase]; if (phase == UITouchPhaseBegan) { [self touchesBegan:touches withEvent:event]; } if (phase == UITouchPhaseEnded) { [self touchesEnded:touches withEvent:event]; } if (phase == UITouchPhaseCancelled) { [self touchesCancelled:touches withEvent:event]; } if (phase == UITouchPhaseMoved) { [self touchesMoved:touches withEvent:event]; }}

Page 33: 303 TANSTAAFL: Using Open Source iPhone UI Code

Coverflow view with OpenFlow

Apple Alex

Page 34: 303 TANSTAAFL: Using Open Source iPhone UI Code

Coverflow view with OpenFlow

AppleMy mods of Alex’s

awesome code (ongoing)

Page 35: 303 TANSTAAFL: Using Open Source iPhone UI Code

Demoor... Julia Child impression

Page 36: 303 TANSTAAFL: Using Open Source iPhone UI Code

To Do

The center view doesn’t quite center after you throw the view around. (UIScrollview’s friction stops where it wants to)

Would like to add flipping the front cover to another view and add the rest of the UI in Apple’s implementation.

Page 37: 303 TANSTAAFL: Using Open Source iPhone UI Code

My mods are available on github at:https://github.com/jonmarimba/OpenFlow

Page 38: 303 TANSTAAFL: Using Open Source iPhone UI Code

Other implementations

FlowCover (OpenGL ES)

Plausible Labs has one, too (Not free, but very accurate)

Page 39: 303 TANSTAAFL: Using Open Source iPhone UI Code

Look for gogoDocs Google Docs reader for iPhone and iPad. It’s in an

the app store in your pocket.

[email protected]://jonathansaggau.com/blog

twit: @jonmarimba

Page 40: 303 TANSTAAFL: Using Open Source iPhone UI Code

[email protected]://jonathansaggau.com/blog

twit: @jonmarimba

Nerds for hire // Hiring nerds

Will Code for food.Will, um, food for code.

Page 41: 303 TANSTAAFL: Using Open Source iPhone UI Code