Tuesday, June 23, 2009

iPhone dev Stupidity 27: Rect in different views

When moving a view to it's superview, instead of padding/fixing rect position of a rect:

use convertRect:fromView:

Monday, June 22, 2009

iPhone dev Stupidity 26: Position

for an array index : 0 1 2 3 ...

if you insert between 2 and 3, the index of the inserted element is still 2.

Friday, June 19, 2009

iPhone dev Stupidity 25: Read The Sample Code of Apple

While hunting around for a draw-round-rect function, I forget that there's already a fucnction: fillRoundRect in HeadsUpUI of apple's sample code.

I really should read more code.


Wednesday, June 17, 2009

iPhone dev Stupidity 24: Unit Test Setup

* Sen:Te's guide


Configure the project

1. Add a ‘Unit Tests’ target

From the Project > New Target… menu add a Mac OS X > Cocoa > Unit Test

2. Configure ‘Unit Tests’ through its inspector:

In the ‘Build’ tab delete all ‘User-Defined Settings’ and the ‘Other Linker Flags’ setting.

In the ‘General’ tab add the Foundation and SenTestingKit frameworks

(/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.sdk/Developer/Library/Frameworks/SenTestingKit.framework)

3. Make the application a dependency of ‘Unit Tests’:

In the ‘General’ tab of the application add ‘UnitTests’ as a direct dependency.
Write some tests

4. Avoid compiling tests outside of the simulator:

 #include "TargetConditionals.h" 
 #if !TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR 
 // the SenTestCase implementation goes here... 
 #endif

* StackOverflow discuss:


If you right click on your unit test target and select the "get info" menu you will see your target options. At the bottom of the pane, you'll see a section called "User Defined": remove the entry containing the path to cocoa.h. I don't remember the name of this entry as I removed it, but this fixes the same problem I had before.

I also changed Base SDK to be Device - iPhone OS 2.2 and other linker flags to -framework Foundation -framework SentestingKit

Tuesday, June 16, 2009

iPhone dev Stupidity 23: Keep Iterator Valid when Removing Object

Just the same as C++, when deleting objects in an NSMutableArray from head to tail, the iterator/index after the deleting point will be invalid.

To solve: delete from tail to head.

iPhone dev Stupidity 22: Forget to Remove from Superview

When removing a subview from your array, remember to remove it from superview. Or it will stick on the screen and refuse to refresh.

Monday, June 15, 2009

iPhone dev Stupidity 21: Binary Search

Don't need to re-invent for Obj-C, use :

CFArrayBSearchValues

iPhone dev Stupidity 20: Animation Fast

From StackOverflow:

"Creating 30 new animations a second is a really bad idea, and not how Core Animation was intended to be used. Core Animation was designed around the idea that you tell the system where you want your layer (or layer-backed view) to end up, and it figures out the rest, including how many frames to drop to run the animation in the time frame you provide. The animations themselves will run on a background thread, but I believe there's a little setup that needs to be performed first which takes place on the calling thread (usually the main thread). This setup might take longer than 1/30th of a second, which would overload your main thread.

You goal should be to minimize the amount of interaction with Core Animation. If you have a complex, but scripted, motion path (or other property change), set it all up ahead of time using a CAKeyframeAnimation and just call that animation once. If there's something about the motion of your object that needs to be altered in response to user input, only do that when you get the actual input. Changing a property of a CALayer mid-animation will cause it to smoothly move from the middle of its current motion path to its new destination.

If you work with Core Animation in this fashion, you can simultaneously animate up to 50 moving translucent layers at 60 FPS on the iPhone."


Sunday, June 14, 2009

iPhone dev Stupidity 19: using shark for iPhone


*) Download your app to the iPhone as usual, you want a release build for performance testing
*) Launch the app on the device
*) Launch /Developer/Applications/Performance Tools/Shark in the Finder
*) Choose Sampling>Network/iPhone menu item
*) Choose your device in the window
*) Choose the application from the popup menu under Target so only your app is sampled
*) Click Start button in window
*) Do stuff in test application
*) Click Stop in window

iPhone dev Stupidity 18: Disable iPhoto When iPhone connected

You can disable it from image capture settings.



Tuesday, June 09, 2009

iPhone dev Stupidity 17: Clip Image from CGContext


// create CGImageRef frome UIView context

CGImageRef imageref;

imageref = CGBitmapContextCreateImage(context);


// convert CGImageRef to UIImage;

UIImage *uiImage;

uiImage =[UIImage imageWithCGImage:imageref];

CGImageRelease(imageref);

Monday, June 08, 2009

iPhone dev Stupidity 16: Subview Index

* how to get subview's index?

Iterating the subviews array and compare:

"I think what you want to do is check the subviews NSArray of your superview or UIWindow and check that. I can't remember which is the front, but it is either the first or last object.
Subviews are drawn with the painter's method. The views are drawn in order from farthest to nearest and the last object drawn is "the front.""

Sunday, June 07, 2009

iPhone dev Stupidity 15: Category Limitation

* Detail of category's implementation

Here's a guess:

Category is implemented by attaching additional slots at the tail of the vtbl (virtual function table). It's based on the fact that there's only one copy of vtbl for a class - if we define a category, the compiler could extend the vtbl for several slots for the newly added member functions.


* Why can't category add ivar?

Because for the well-known class like NSObject, all the libraries may use it as base class - and they're in binary. We have no way to change the layout of the object for compiled code.

So it's not able to simulate multi-inheritance gracefully.

Saturday, June 06, 2009

iPhone dev Stupidity 14: UIFont size

To understand the properties of UIFont (from Microsoft doc on opentype ):


 



Log info for a 42 pointsized system font:


> font size 21 50

> Font Helvetica properties: leading 50.000000, ascender 39.340820, capheight 30.534000, descender -9.659180, pointSize 42.000000


Leading: the height of the line.


To get the tight bounding box of a character, height = ascender.


And to center a char at the center of a view:


 


- (CGRect) centerRectForChar: (NSString*) txt{
 CGSize font_size = [text_ sizeWithFontfont_];
 int x = (VIEW_WIDTH/2) - (font_size.width/2);
 int y = (VIEW_HEIGHT/2) - (font_size.height/2);
 int w = font_size.width;
 int h = font_.ascender;

 return CGRectMake(x, y, w, h);
}

Thursday, June 04, 2009

iPhone dev Stupidity 13 - Transparent View

* how to cover a transparent/Glass view onto an opaque view?

The trick from Jeff LaMarche's Blog:

"here's the point to remember today: if you need objects below yours to show through parts of your view without using alpha, the backgroundColor property MUST be set to [UIColor clearColor]. This is true even if you do not call drawRect on super.

The other thing you need to do is make sure that the 
opaque property is set to NO. Ordinarily, opaque should be set to YES as an optimization, but that optimization isn't available to us if we want stuff to show through from below."

Messing with alpha does show the view under cover through - but with kind of sick effect: everything is just blurred. If you want to draw something on this glass view, prefer to Jeff's approach.

iPhone dev Stupidity 12

* awakeFromNib vs initWithFrame


 "Note that you have to implement this and not using the initWithFrame method that the x-code template gives you as nib controlled views don't use initWithFrame (from the apple documentation)."