Having Fun With The iPhone SDK
I have been playing with the iPhone SDK since the spring time and it’s a lot of fun to program for the platform. There is something to be said for having something tangible at the end of the day. You can be anywhere and pull out your phone and say “Hey, check out what I have been working on”.
I have been porting over some Flash code libraries and games to it, which has been interesting because there are some differences on how Objective-C works compared to Flash.
The first and potentially most obvious is positioning objects. The counterpart to DisplayObject in the SDK is UIView. There are several properties that allow you to position objects, but the one you want to use is UIView.center. This is where Flash and the IPhone SDK differ a little because Flash, usually, deals with x,y in the top left, where the SDK deals with the registration point in the center. When you are porting over design and graphics from the Flash and laying everything out in Flash it’s easier to deal with top left instead of doing all the conversion to the center. Here is some methods that I wrote that subclasses UIView. I should also mention that self in Objective-C is this in Flash.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | -(void)x:(float)_x { self.center = CGPointMake( _x + ( self.bounds.size.width / 2 ), self.center.y ); } -(void)y:(float)_y { self.center = CGPointMake( self.center.x, _y + ( self.bounds.size.height / 2 ) ); } -(float)y { return self.center.y - ( self.bounds.size.height / 2 ); } -(float)x { return self.center.x - ( self.bounds.size.width / 2 ); } |
You can also use the frame property to position items, but things will go a little wonky when trying to do a CGAffineTransform on the object at the same time you are position it. For example, rotating the object at the same time you are animating it’s position.
Some of you might be saying we’ll why don’t you use Interface Builder to lay everything out? The biggest reason is that we only have a few people working on OSX so the designers don’t have access to it. But I will say that Interface Builder is a wicked application and I am hoping Thermo lives up to it.




