Thursday, January 07, 2010

iPhone dev Stupidity 120: Assoc view and it's animation

1. associate view to the animation:

[group setValue:viewForAnimation forKey: kTrashItemAnimation];


2. associate animation to the view:

[viewForAnimation.layer addAnimation:group forKey:@"savingAnimation"];


later in the animation did stop callback, we can get the view from the animation ended to do more works based on different views.

iPhone dev Stupidity 119: Table row deletion order

You need to delete the row in the data model first. Then you can delete it in the UITableView.

For UITableView will check the sanity of the data model when deleting rows, there'll be an error if the sanity check fails.

Tuesday, January 05, 2010

iPhone dev Stupidity 118: How to animate a UIBarButtonItem

1. The animation
 You need to setup a UIBarButtonItem with customView. That could be a UIImageView. The fire a timer to call toolbar's setItems:animated: method.

2. How to get those animation images?

The Other.artwork file is in /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/System/Library/Frameworks/UIKit.framework/ (you need the SDK).
Use the "iPhoneShop-1.3.jar" program -- available currently here to extract all of the images into a directory.

    java -jar iPhoneShop-1.3.jar ARTWORK Other.artwork EXPORT Other

* A single fact: 

The trashing animation of iPhoto takes 15 frames to open the trash bin and another 16 frames to close it.

Sunday, January 03, 2010

iPhone dev Stupidity 117: Animation along curved path

From stackoverflow:


The best way to handle this is to use Core Animation to animate the motion of the image or view along a Bezier path. This is accomplished using a CAKeyframeAnimation. 

Tuesday, December 29, 2009

iPhone dev Stupidity 116: How to find out what mach_msg_trap wait

from stackoverflow:


"Turn out my app is not actually spending 65% of it's time in the mach_msg_trap function. It was a configuration error in Instruments on my part.

The Sampler tool defaults to All Sample Counts, this will measure all threads regardless of their state.

Instead switch to Running Sample Times that will reflect the current actual workload."

Shark has similar options:


Wednesday, December 16, 2009

iPhone dev Stupidity 115: UITableView delete section

When items of a section are deleted, the section is deleted automatically - no need to write section delete code like:


[self.tableView deleteSections: [NSIndexSet indexSetWithIndex: section] withRowAnimation: UITableViewRowAnimationLeft];


Tuesday, December 15, 2009

iPhone dev Stupidity 114: Muliti column order

1. to order by two columns

From here: 

SELECT * FROM Individual
ORDER BY FirstName, LastName

2. We want to sort files by name - when two files have the same name we sort them by the date time:

NSString * sql = [NSString stringWithFormat: @"SELECT arts.*, tags.name FROM arts, tags WHERE category_id = %d and arts.tag_1 = tags.tag_id and ((tags.name = '%@' and arts.date_touched > '%@') or tags.name > '%@') ORDER BY tags.name LIMIT 1", cur_art.category_id_, cur_name, cur_art.date_touched_, cur_name];



iPhone dev Stupidity 113: Write protection of Resource

In the device, the resource/library folder is read-only. In the simulator, there's no protection for the folder.

Sunday, December 13, 2009

iPhone dev Stupidity 112: SQLite, Last inserted row id

From Widipedia:


Database designers that use a surrogate key as the primary key for every table will run into the occasional scenario where they need to automatically retrieve the database generated primary key from a SQL INSERT statement for use in another SQL statements. Most systems do not allow SQL INSERT statements to return row data. Therefore, it becomes necessary to implement a workaround in such scenarios.

Using a database-specific stored procedure that generates the surrogate key, performs the INSERT operation, and finally returns the generated key. For example, in Microsoft SQL Server, the key is retrieved via the SCOPE_IDENTITY() special function, while in SQLite the function is named last_insert_rowid().

Wednesday, December 09, 2009

iPhone dev Stupidty 111: SQL table order

From here:

"In a relational database... rows have no order! This cannot be stressed enough.  I don't care how you order the rows, SQL Server is under no obligation to return the data in that order.  And frankly, even if you order the data using the clustered techniques mentioned, this only guarantees it when you rebuild it.  After that, SQL Server may split pages, and then you technically don't have a flat structure anymore. "

Monday, November 30, 2009

iPhone dev Stupidity 110: Fine Grained Observing for Text Field

1. Using Key-value Observing to get update for a UITextField editing like this:

[nameField_ addObserver: self forKeyPath: @"text" options: NSKeyValueObservingOptionPrior context: nil];

You will only get the observing method (- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context) called when the edit is done - the text field lost focus.

2. To observe each character input, we need NSNotificationCenter (from apple forum):

[[NSNotificationCenter defaultCenter] addObserver: self  selector: @selector(textFieldTextDidChange:) name: @"UITextFieldTextDidChangeNotification"  object: nil];


where to find out the @"UITextFieldDidChangeNotification" string? It's in the UITextField.h:

UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification;


UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification;


UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;


and sure apple had started iPhone SDK in 2005:


//  UITextField.h


//  UIKit


//


//  Copyright 2005-2009 Apple Inc. All rights reserved.


iPhone dev Stupidity 109: NSAttributedString, NSFormatter for UIT

1. from stackoverflow : NSAttributedString is not on iPhone.

2. from iPhone Dev SDK : You can't plug an NSFormatter onto a UITextField - thus you can't provide a partial string when input in text field:
is not available on iPhone. 





Sunday, November 29, 2009

iPhone dev Stupidity 108: Scrolled UITextField

If a text field get focused during scrolling, the scrolling will stop.

So better to focus it when the scroll animation is done: 
 * for program triggered scroll: - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
 * for finger dragged scroll: - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 

Saturday, November 28, 2009

iPhone dev Stupidity 107: LuaFileSystem

Some dependency:





To use LuaFileSystem, we need LuaRocks to install the lfs package.

LuaRocks relies on wget to parse the rock specs (not available on Mac OS X).

lfs will build into a lfs.so. If necessary, you need to copy it to 'require' search paths of lua and add the access rights to your user/usergroup.


iPhone dev Stupidity 106: Protocal for Category, Override


iPhone dev Stupidity 105: Confuse alpha = 0.0 with remove

To avoid flicker when removing a view, you might use an UIView animation to set a alpha = 0.0 and remove the view in the animation stop callback.

Make sure to assert(view.retainCount == 1) before removing. For if you have reference count error (retainCount > 1) and the view will not get removed - but you can't see it since the alpha has set to 0.0.

Of course, you can set the alpa = 0.5 in debug mode to "see" if it's removed finally.

Wednesday, November 25, 2009

iPhone dev Stupidity 104: Update Navigation Bar

from apple help doc:

When the user changes the top-level view controller, whether by pushing or popping a view controller or changing the contents of the navigation stack directly, the navigation controller updates the navigation bar accordingly. Specifically, the navigation controller updates the bar button items displayed in each of the three navigation bar positions: left, middle, and right. Bar button items are instances of the UIBarButtonItem class. You can create items with custom content or create standard system items depending on your needs. For more information about how to create bar button items, seeUIBarButtonItem Class Reference.

Check the doc for detailed rules for the left, middle and right view updating.


A UINavigationBar object uses a stack to manage navigation items (instances of UINavigationItem) that represent a state of the navigation bar. You change the navigation bar by pushing navigation items using the pushNavigationItem:animated: method or popping navigation items using thepopNavigationItemAnimated: method. Methods with an animated: argument allow you to animate the changes to the display.


and some error:

2009-11-26 07:45:11.742 Flip[404:20b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.'


Monday, November 23, 2009

iPhone dev Stupidity 102: Do you have to release IBOutlet?

from stackoverflow:

Your IBOutlets are probably @properties. If they are, and you have retain as an attribute, then you do need to release in -dealloc

A best practice: 

self.myoutlet = nil; in dealloc

This works for both the retained and assigned IBOutlet. The retained will release and the assigned will do nothing.


iPhone dev Stupidity 103: NSZombie and Memory leaks

from here:

NSZombieEnabled creates leaks ON PURPOSE so you can track down calls to objects that are released prematurely. When you run with NSZombieEnabled, objects are never dealloced. When it's time to dealloc the object, NSZombieEnabled tells the runtime to turn the object into an instance of NSZombie. Any attempt to message an NSZombie will result in a runtime exception. Always remember to remove the NSZombieEnabled flag before sending an app into production! Due to the fact that dealloc is overridden, your app will never free any memory - it will just grow and grow. It's for testing and debugging only.


iPhone dev Stupidity 101: How to draw text vertically?

1. Drawing NSString in unusual direction : limited to ascii string and have effect like this:

    use CGContextShowText(ctxt, tbuf.c_str(), tbuf.length()); and CGAffineTransform 

2. CGFontGetGlyphsForUnichars

   to draw unichar we need this function to get glyph out of a unichar. Then core graphic has a set of function for showing glyph like this one:


void CGContextShowGlyphsAtPoint (
CGContextRef c,
CGFloat x,
CGFloat y,
const
CGGlyph glyphs[],
size_t count
);


but CGFontGetGlyphsForUnichars is a private API for iPhone. So we got:

3. A replacement for CGFontGetGlyphsForUnichars

on github

-----

4. bonus: draw gradient filled UILabel.

also uses: extern void CGFontGetGlyphsForUnichars(CGFontRef, const UniChar[], const CGGlyph[], size_t);