<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description></description><title>kwigbo</title><generator>Tumblr (3.0; @kwigbo)</generator><link>http://kwigbo.com/</link><item><title>Turn Off Xcode Undo Warning</title><description>&lt;p&gt;I am not sure how many other people get irritated when trying to undo in Xcode. When you save a file you always get a prompt that asks if you want to undo after a save. Running this simple command in Terminal will get rid of that annoying warning.

&lt;pre class="brush:cpp"&gt;
defaults write com.apple.Xcode XCShowUndoPastSaveWarning NO
&lt;/pre&gt;

You will have to restart Xcode after changing this setting.&lt;/p&gt;</description><link>http://kwigbo.com/post/880025146</link><guid>http://kwigbo.com/post/880025146</guid><pubDate>Fri, 30 Jul 2010 11:20:06 -0400</pubDate><category>snippet</category><category>xcode</category></item><item><title>Weezy World An Adventure Game</title><description>&lt;p&gt;I am in the initial planning phase of a new game Weezy World. This is a working title and I don’t know if I will change it. The general idea of it is to create a Kings Quest V style game with a twist. I love top down tile based games in the perspective of Zelda. I was thinking it would be cool to have that type of world but have the control scheme of Kings Quest V. For those of you not familiar with Kings Quest the controls would be a slide out drawer of action commands. Look, touch, walk, run, active item and inventory. So, you would pick an action and then tap the screen to execute that action. For example, to walk across the screen you need to choose the walk command and tap on where you want to walk to. If you want to examine a suspicious tree you would choose look. Dialogs would be shown for conversations with people or the results of examining an object in the world. The idea of the game is to explore the world and find different objects to help you complete tasks and move through the story. I really like the idea of doing this type of game in a different perspective. I am also leaning towards a pixel art based game. I have had this idea running through my head for years now and I think it’s finally time to build it. Please let me know what you think via the comments.&lt;/p&gt;</description><link>http://kwigbo.com/post/823764235</link><guid>http://kwigbo.com/post/823764235</guid><pubDate>Sat, 17 Jul 2010 10:01:24 -0400</pubDate></item><item><title>UITableViewCell Multiline Text</title><description>&lt;p&gt;&lt;pre class="brush:cpp"&gt;
- (UITableViewCell *)tableView:(UITableView *)aTableView 
                cellForRowAtIndexPath:(NSIndexPath *) indexPath
{		
	UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:@"Cell"];
	
	if(cell == nil)
	{
		cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		
		cell.textLabel.font = [UIFont fontWithName:@"Arial" size:18];
		cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
		cell.textLabel.numberOfLines = 3;
	}
	
	return cell;
}
&lt;/pre&gt;&lt;/p&gt;</description><link>http://kwigbo.com/post/816314143</link><guid>http://kwigbo.com/post/816314143</guid><pubDate>Thu, 15 Jul 2010 15:47:00 -0400</pubDate><category>snippet</category></item><item><title>OpenFeint Multiplayer Integration Into Artifice</title><description>&lt;p&gt;So, OpenFeint finally added turn based multiplayer into their SDK. I can now finally ditch Google App Engine and Urban Airship! WOO HOO!!! Don’t get me wrong, these are not bad services. I have actually found them to be quite nice. They can however both cost money, and the App Engine app is more code to maintain. Personally I would prefer to work on the client side over the server side any day. With the new OpenFeint multiplayer SDK I can do just that. &lt;/p&gt;

&lt;p&gt;What follows is a quick rundown of the OpenFeint multiplayer system. This systems seems to be rather flexible and the way I used it is not the only way to do things.&lt;/p&gt;

&lt;p&gt;More after the jump!&lt;/p&gt;

&lt;!-- more --&gt;

&lt;h3&gt;Where to start?&lt;/h3&gt;

&lt;p&gt;The first thing you have to do is initialize the OpenFeint multiplayer service. This is as simple as specifying a delegate. For my delegate I used one of the other OpenFeint delegates I was already using.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
// Pass a reference to whatever object you want to house the delegate methods you will need later
[OFMultiplayerService setDelegate:openFDelegate];
&lt;/pre&gt;

&lt;p&gt;I have decided to use the lobby approach for Artifice. This is used in the MPSampleApp that comes with the OpenFeint multiplayer SDK. I also decided that I would allow the user to have up to 10 games of Artifice running at one time. To do this we need to set the slot array size for the OFMultiplayerService.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
[OFMultiplayerService setSlotArraySize:10];
&lt;/pre&gt;

&lt;p&gt;These two setup method calls I have inside my app delegate right after my OpenFeint SDK initialization.&lt;/p&gt;

&lt;p&gt;The delegate methods I needed to use are gameMoveReceived: and netWorkDidUpdateLobby. The first is called when an opponent makes a move. I will return to this later when I talk about making moves. The second method is called when a change has been made to one of your currently running games. In this method I simply post a notification to be caught by my lobby UI.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
//Called when a move was received.
- (BOOL) gameMoveReceived:(OFMultiplayerMove  *) move
{
	if(move.code == OFMP_MC_DATA)
		[Artifice postGotMove:[move data]];
	
	return YES;
}

//Called when the games are updated from the network.
- (void) networkDidUpdateLobby
{
	[Artifice postOFNetworkDidUpdateLobby];
}
&lt;/pre&gt;

&lt;h3&gt;Creating A Game&lt;/h3&gt;

&lt;p&gt;The next thing on our list is to start creating games. For this I pretty much followed the MPSampleApp example. I created a UIViewControler with a UITableView to hold the available game “slots”. When viewing and editing your game slots you need to “view” your games. You also need to stop viewing them when you are done. This is done in the sample app with viewDidLoad: and viewDidDisapear:. Viewing the games will have the networkDidUpdateLobby called which can be used to check the state of your game slots.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
- (void) viewDidLoad
{
	[OFMultiplayerService startViewingGames];
}

-(void)viewDidDisappear:(BOOL)animated
{
	[OFMultiplayerService stopViewingGames];
}
&lt;/pre&gt;

&lt;p&gt;Next some buttons can be created to execute the different actions on each slot. The Actions are as follows …&lt;/p&gt;

&lt;h4&gt;Create&lt;/h4&gt;
&lt;p&gt;The create command does just as it says, it will create a new game. There will be no opponent assigned when this is executed. This will allow a random person to join this game. When we create a new game we can simply use the OFMultiplayerService method getSlot:. We pass this an index from 0-9 in my case and it will return one of our 10 available OFMultiplayerGame objects. We then call the createGame:withOptions: method on the game object returned. The OF_GAME_DEFINITION_ID to my understanding is just an identifier for your game, in my case Artifice. As for the config, in my case there were only a few settings. Each game has a maximum and minimum of 2 players. The LOBBY_OPTION_CONFIG setting is used when finding a game.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
OFMultiplayerGame* game = [OFMultiplayerService getSlot:slotIndex];
		
if(![game isActive])
{
	NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
		@"GAME CONFIG", OFMultiplayer::LOBBY_OPTION_CONFIG,
		[NSNumber numberWithUnsignedInt:2], OFMultiplayer::LOBBY_OPTION_MAX_PLAYERS,
		[NSNumber numberWithUnsignedInt:2], OFMultiplayer::LOBBY_OPTION_MIN_PLAYERS,
		nil];
			
	[game createGame:OF_GAME_DEFINITION_ID withOptions:options];
}
&lt;/pre&gt;

&lt;h4&gt; Find &lt;/h4&gt;
&lt;p&gt;The find command will be used to find a game that has been created but has no opponent. Here we see the  OF_GAME_DEFINITION_ID again, we use this to find a game of Artifice on the OpenFeint servers. We simply grab one of our OFMultiplayerGame objects with the OFMultiplayerService class. Then you call the findGame:withOptions: method and a game will be found and the client be assigned as an opponent.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
OFMultiplayerGame* game = [OFMultiplayerService getSlot:slotIndex];
[game findGame:OF_GAME_DEFINITION_ID withOptions:nil];
&lt;/pre&gt;

&lt;h4&gt;Challenge&lt;/h4&gt;
&lt;p&gt;With this action you can challenge one of your OpenFeint friends to a game of Artifice. OpenFeint has finally added an easy way to show the friend picker. What took me about 5 lines of code before is now down to one. With this method you just specify a delegate, some text for the pickers title and an application id (to filter out people who don’t own the game). &lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
[OFFriendPickerController launchPickerWithDelegate:self promptText:@"Choose an opponent" mustHaveApplicationId:@"910"];
&lt;/pre&gt;

&lt;p&gt;With the exception of one setting, this is the same as our create action.  The new setting is to take the chosen user from the picker and set them as the opponent.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
OFMultiplayerGame* game = [OFMultiplayerService getSlot:slotIndex];
		
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
	@"GAME CONFIG", OFMultiplayer::LOBBY_OPTION_CONFIG,
	[NSNumber numberWithUnsignedInt:2], OFMultiplayer::LOBBY_OPTION_MAX_PLAYERS,
	[NSNumber numberWithUnsignedInt:2], OFMultiplayer::LOBBY_OPTION_MIN_PLAYERS,
	[NSArray arrayWithObject:[selectedUser resourceId]], OFMultiplayer::LOBBY_OPTION_CHALLENGE_OF_IDS,
	nil];
		
[game createGame:OF_GAME_DEFINITION_ID withOptions:options];
&lt;/pre&gt;

&lt;h4&gt;Play&lt;/h4&gt;
&lt;p&gt;Our play action will allow us to start sending moves for a particular game slot. We grab the OFMultiplayerGame and check if the game has started (the challenge has been accepted). If it has we load out game UI and start playing. If it has not been started we check if it currently holds a challenge from another player. If it does we accept the challenge and wait for them to go.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
OFMultiplayerGame *game = [OFMultiplayerService getSlot:slotIndex];
		
if([game isStarted])
{
	// Load game UI
}
else if([game hasBeenChallenged])
{
	[game sendChallengeResponseWithAccept:YES];
}
&lt;/pre&gt;

&lt;h4&gt;Cancel&lt;/h4&gt;
&lt;p&gt;Finally, our cancel action will either refuse a challenge or cancel and delete the game.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
OFMultiplayerGame *game = [OFMultiplayerService getSlot:slotIndex];
		
if([game hasBeenChallenged])
	[game sendChallengeResponseWithAccept:NO];
else
	[[OFMultiplayerService getSlot:path.row] cancelGame];
&lt;/pre&gt;

&lt;h3&gt;Enter/Leave A Game&lt;/h3&gt;

&lt;p&gt;In order to start and stop receiving moves for a particular game you need to call the following methods. For instance, in Artifice I call enterGame: when I load the board UI and I call leaveGame when I close the board UI.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
OFMultiplayerGame *game = [OFMultiplayerService getSlot:slotIndex];
		
[OFMultiplayerService enterGame:game];
[OFMultiplayerService leaveGame];
&lt;/pre&gt;


&lt;h3&gt;Taking a Turn&lt;/h3&gt;

&lt;p&gt;Now that we have a game created and a challenge has been accepted we need to start playing. For this we grab the current game we want to make a move for and call the method sendMove:. This method takes an instance of NSData, which can be used to pass the current users turn information to the opponent. In my case I simply created a game object that can be serialized and deserialize. This way I can just pass the current state back and forth via the sendMove: method.  After sending the actual data I then call the sendEndTurnWithPushNotification: method. This will allow me to send a push notification with a specified message to the opponent.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
OFMultiplayerGame *game = [OFMultiplayerService getSlot:activeSlot];
[game sendMove:[game gameData]];
[game sendEndTurnWithPushNotification:[NSString stringWithFormat:@"%@ went, it's your turn!", [OpenFeint lastLoggedInUserName]]];
&lt;/pre&gt;

&lt;p&gt;OpenFeint recommends that you wrap your send move method calls inside an if statement checking the isReadyToSendMoves method of the OFMultiplayerService class. I had problems with that, more on this later.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
[OFMultiplayerService isReadyToSendMoves];
&lt;/pre&gt;

&lt;p&gt;Now, back to the delegate method we saw at the beginning. Since the sendEndTurnWithPushNotification: is technically counted as a move I wanted to filter them out. I just needed the move that contained the data for the game. To do this we can just check the code property of the OFMultiplayerMove object received. I then take that and dispatch a notification to be received by my game UI. Once the game UI has the data it can deserialize it and load it into view.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
//Called when a move was received.
- (BOOL) gameMoveReceived:(OFMultiplayerMove  *) move
{
	if(move.code == OFMP_MC_DATA)
		[Artifice postGotMove:[move data]];
	
	return YES;
}
&lt;/pre&gt;

&lt;h3&gt;Ending A Game&lt;/h3&gt;

&lt;p&gt;When you want to end the game you need to know what order your players are in. You can use the game.playerOFUserIds of the OFMultiplayerGame object to do this. First I check to see if the current player is the first id in the playerOFUserIds array. The we do a test to see if the current player is the winner or loser. We then create an NSNumber with either a 0 (lose) or a 1 (win) for each of our players in the game. After we have our “ranks” we can add them to an array and pass them to OFMultiplayerService finishGameWithPlayerRanks: method. This will end the game.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
NSMutableArray *players = game.playerOFUserIds;
BOOL firstIsMe = [[players objectAtIndex:0] isEqualToString:[OpenFeint lastLoggedInUserId]];

// Test if the current player is the winner
BOOL iWin = YES;

NSNumber * me = iWin ? [NSNumber numberWithUnsignedInt:1] : [NSNumber numberWithUnsignedInt:0];
NSNumber * you = iWin ? [NSNumber numberWithUnsignedInt:0] : [NSNumber numberWithUnsignedInt:1];

if(firstIsMe) [OFMultiplayerService finishGameWithPlayerRanks:[NSArray arrayWithObjects:me, you, nil]];
else [OFMultiplayerService finishGameWithPlayerRanks:[NSArray arrayWithObjects:you, me, nil]];
&lt;/pre&gt;

&lt;h3&gt;Final Thoughts&lt;/h3&gt;

&lt;p&gt;All in all I like the OpenFeint multiplayer service. It still seems very flexible and nice to work with. There are some issues I have run into though. For instance sometimes when I accept a challenge it will empty the game slot on both clients (There is a fix for this in the comments, thanks Ron!). Another problem that I came across was in receiving moves. The gameMoveReceived: method returns a BOOL if the move has been processed. Although I process all the moves, deep in the OpenFeint code the mIncomingMoveQueue.count never drops to zero. This makes isReadyToSendMoves always return false. The third issue that I found is that sometimes a client will receive it’s own moves. This caused problems in my game since it makes the move happen twice (I was able to get OpenFeint the debug info needed to fix this issue, it should be coming soon).&lt;/p&gt;

&lt;p&gt;This post is my attempt at better understanding the work I just did. There may be mistakes and please feel free to point them out if you see any.&lt;/p&gt;</description><link>http://kwigbo.com/post/814867252</link><guid>http://kwigbo.com/post/814867252</guid><pubDate>Thu, 15 Jul 2010 07:28:00 -0400</pubDate><category>tutorials</category></item><item><title>UIToolBar Hide / Show UIBarButtonItem</title><description>&lt;p&gt;&lt;pre class="brush:cpp"&gt;
BOOL active = YES;
	
NSMutableArray *items = [NSMutableArray arrayWithArray:toolBar.items];
	
BOOL found;
	
for(int i = 0; i &lt; items.count; i++)
{
     UIBarButtonItem *item = (UIBarButtonItem *)[items objectAtIndex:i];
     if([item isEqual:myButton]) found = YES;
}
	
if(found &amp;&amp; !active) [items removeObjectAtIndex:myButtonIndex];
if(!found &amp;&amp; active) [items insertObject:textViewButton atIndex:myButtonIndex];
	
[toolBar setItems:items animated:YES];
&lt;/pre&gt;&lt;/p&gt;</description><link>http://kwigbo.com/post/810806869</link><guid>http://kwigbo.com/post/810806869</guid><pubDate>Wed, 14 Jul 2010 09:08:00 -0400</pubDate><category>snippet</category></item><item><title>NSNotification Management</title><description>&lt;p&gt;Coming from the world of Flash/Actionscript I really like the concept of NSNotifications in iPhone apps. Problem is, keeping track of your notifications can become a pain real quick. That is why I like to create a centralized location to manage all my NSNotifications. More after the jump.&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;First we will create our header. (Read comments)&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
@interface ApplicationNotifications : NSObject
{
}

// This will be our constants for our first notifications
extern NSString *const MY_FIRST_NOTIFICATION;
extern NSString *const MY_FIRST_NOTIFICATION_WITH_STRING;

// We will call these to post our notifications
+ (void) postMyFirstNotification;
+ (void) postMyFirstNotificationWithString:(NSString *) string;

// These will be called to allow objects to listen to and stop listening for notifications
// These are just a convenience and not required
+ (void) addObserver:(id) observer selector:(SEL) selector name:(NSString *) name;
+ (void)removeObserver:(id)notificationObserver name:(NSString *)notificationName;
+ (void) removeObserver:(id) observer;

@end
&lt;/pre&gt;

&lt;p&gt;Next we will write our implementation. (Read comments)&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
#import "ApplicationNotifications.h"

@implementation ApplicationNotifications

// Here we define the values for our constants
NSString *const MY_FIRST_NOTIFICATION = @"MyFirstNote";
NSString *const MY_FIRST_NOTIFICATION_WITH_STRING = @"MyFirstNoteWithString";

// Here we just call the defaultCenter and post a notification with our constant name.
+ (void) postMyFirstNotification
{
	[[NSNotificationCenter defaultCenter] postNotificationName:MY_FIRST_NOTIFICATION object:nil];
}

// Same as the previous method, we just pass an NSString along with the notification.
+ (void) postMyFirstNotificationWithString:(NSString *) string
{
       [[NSNotificationCenter defaultCenter] postNotificationName:MY_FIRST_NOTIFICATION object:string];
}

// Here are our convenience methods for adding and removing observers
+ (void) addObserver:(id) observer selector:(SEL) selector name:(NSString *) name
{
	[[NSNotificationCenter defaultCenter] addObserver:observer selector:selector name:name object:nil];
}

+ (void)removeObserver:(id)notificationObserver name:(NSString *)notificationName
{
	[[NSNotificationCenter defaultCenter] removeObserver:notificationObserver name:notificationName object:notificationSender];
}

+ (void) removeObserver:(id) observer
{
	[[NSNotificationCenter defaultCenter] removeObserver:observer];
}

@end
&lt;/pre&gt;

&lt;p&gt;When you want to post one of your notifications it’s as simple as importing the ApplicationNotifications class and calling one of our post methods.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
[ApplicationNotifications postMyFirstNotification];
[ApplicationNotifications postMyFirstNotificationWithString:@"mystring"];
&lt;/pre&gt;

&lt;p&gt;How do we listen for these notifications? All we need to do is import our ApplicationNotifications class and call the addObserver method&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
[ApplicationNotifications addObserver:self selector:@selector(myFirstNote) name:MY_FIRST_NOTIFICATION];

// Notice the : in the selector in this one. We need this to get the NSNotification
// object to grab our string from.
[ApplicationNotifications addObserver:self selector:@selector(myFirstNoteWithString:) name:MY_FIRST_NOTIFICATION_WITH_STRING];
&lt;/pre&gt;

&lt;p&gt;Next we setup our methods that will be called. These will match the selectors specified in the last step.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
- (void) myFirstNote
{
    // handle notification here
}

- (void) myFirstNoteWithString:(NSNotification *) note
{
    // Here we grab our string from the notification. we could also pass any other custom
    // object along with the notification.
    NSString *stringFromNote = (NSString *)[note object];
}
&lt;/pre&gt;

&lt;p&gt;We alos need to clean up our notifications when we don’t need them anymore. We can either clean them up one at a time when not needed or all at once for an individual observer.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
// This is good to call in the dealloc method to stop the object from observing all notes
[ApplicationNotifications removeObserver:self];
// This one is used if at some point in the life of our application we just want
// to stop listening for a single notification
[ApplicationNotifications removeObserver:self name:MY_FIRST_NOTIFICATION];
&lt;/pre&gt;

&lt;p&gt;When it comes down to it the most convenient part of this is when dispatching NSNotification. The rest of it is just there for consistency. As always if there is any confusion please ask questions in the comments section below. Thanks!&lt;/p&gt;</description><link>http://kwigbo.com/post/761434700</link><guid>http://kwigbo.com/post/761434700</guid><pubDate>Fri, 02 Jul 2010 08:45:00 -0400</pubDate><category>tutorials</category><category>snippet</category></item><item><title>UIScrollView Image Gallery Tutorial</title><description>&lt;p&gt;Using a UIScrollView to create a paging application seems to be a question I am frequently asked about. I decided to create this tutorial to show how you can create an image gallery with a UIScrollView. I will show you have to reuse two UIImageViews views to avoid creating a UIImageView for each image in the gallery. Full tutorial and source after the jump.&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;I will highlight the most important bits of code here. The rest can be downloaded at the end of this post.&lt;/p&gt;

&lt;p&gt;In the init method we will create our UIScrollView and two UIImageViews. The UIScrollView will be added to the UIViewControllers view. Each of the UIImageViews will be added as a child of the UIScrollView. We make sure that paging is enabled to give it that default iPhone paging effect. We also want the directionalLockEnabled to prevent dragging in other directions once dragging has begun. The scroll indicators are hidden but this is not a requirement. Our UIViewController is then set as the delegate for the UIScrollView so we know when scrolling has occurred.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
- (id) init
{
	if(self = [super init])
	{
		scroll = [[UIScrollView alloc] init];
		scroll.scrollEnabled = YES;
		scroll.pagingEnabled = YES;
		scroll.directionalLockEnabled = YES;
		scroll.showsVerticalScrollIndicator = NO;
		scroll.showsHorizontalScrollIndicator = NO;
		scroll.delegate = self;
		scroll.backgroundColor = [UIColor blueColor];
		scroll.autoresizesSubviews = YES;
		scroll.frame = CGRectMake(0, 0, 320, 480);
		[self.view addSubview:scroll];
		
		view1 = [[UIImageView alloc] init];
		[scroll addSubview:view1];
		
		view2 = [[UIImageView alloc] init];
		[scroll addSubview:view2];
	}
	
	return self;
}
&lt;/pre&gt;

&lt;p&gt;In our UIViewController we are going to implement the scrollViewDidScroll method. I created an update method that is called from here. This is just a personal preference based on my own organizational habits. I will go over the update method last.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
- (void) scrollViewDidScroll:(UIScrollView *) scrollView
{	
	[self update];
}
&lt;/pre&gt;

&lt;p&gt;Next up is out setImages method. This method is public and will be called from the UIViewControllers parent in order to load our images. With this methods we can pass in an NSArray of UIImage objects. If a set of images has already been added we will release the current set and store the new set. In this method we will also set the initial location of our UIImageViews by altering their frame. We will also load the first images from our new set of images into our UIImageViews. Last in this method we will set the contentSize property of the UIScrollView. This will tell our UIScrollView how much content to expect. (Note: I am using hard coded values of 320x480 as the screen size. This can be altered to grab the actual size of the screen if needed.)&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
- (void) setImages:(NSArray *) images
{
	if(imageSet) [imageSet release];
	
	imageSet = [images retain];
	
	view1.frame = CGRectMake(0, 0, 320, 480);
	view2.frame = CGRectMake(320, 0, 320, 480);
	
	view1.image = [imageSet objectAtIndex:0];
	view2.image = [imageSet objectAtIndex:1];
	
	scroll.contentSize = CGSizeMake([imageSet count]*320, 480);
}
&lt;/pre&gt;

&lt;p&gt;The last step in this is to update the positions of our UIImageViews and reload the images as it scrolls. In this method we take the screen width and the current position of the UIScrollView to calculate which index in our images array is currently visible. We use the &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Modulo_operation"&gt;modulo operation&lt;/a&gt; to figure out which UIImageView we should use. If the index is 0 we should use view1, if the index is 1 we should use view 2, if the index is 2 we should use view1 etc. We also calculate the true position. By this I mean the x value of the currently visible UIImageView. We can now figure out which index is next based on the truePosition and the position the UIScrollView is currently at. We also need to make sure that our next index isn’t out of range and is within the bounds of our images array. We also want to make sure we aren’t reloading and repositioning an image that has already been set. Once everything is calculated we can then update the UIImageView with the new image based on the next index and set the position based on that index as well.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
- (void) update
{
	CGFloat pageWidth = 320;
	float currPos = scroll.contentOffset.x;
	
	int selectedPage = roundf(currPos / pageWidth);
	
	float truePosition = selectedPage*pageWidth;
	
	int zone = selectedPage % 2;
	
	BOOL view1Active = zone == 0;
	
	UIImageView *nextView = view1Active ? view2 : view1;
	
	int nextpage = truePosition &gt; currPos ? selectedPage-1 : selectedPage+1;
	
	if(nextpage &gt;= 0 &amp;&amp; nextpage &lt; [imageSet count])
	{
		if((view1Active &amp;&amp; nextpage == view1Index) || (!view1Active &amp;&amp; nextpage == view2Index)) return;
		
		nextView.frame = CGRectMake(nextpage*320, 0, 320, 480);
		nextView.image = [imageSet objectAtIndex:nextpage];
		
		if(view1Active) view1Index = nextpage;
		else view2Index = nextpage;
	}
}
&lt;/pre&gt;

&lt;p&gt;That’s the end, I hope everything makes sense. This was my first tutorial so I hope it turned out well. Please let me know if there are any question via the comments. The source code for the full project can be downloaded below.&lt;/p&gt;

&lt;a target="_blank" href="http://kwigbo.s3.amazonaws.com/source/PageRenderer.zip"&gt;Source Code&lt;/a&gt;</description><link>http://kwigbo.com/post/758575763</link><guid>http://kwigbo.com/post/758575763</guid><pubDate>Thu, 01 Jul 2010 15:15:00 -0400</pubDate><category>source code</category><category>tutorials</category></item><item><title>UITextView resignFirstResponder If isFirstResponder</title><description>&lt;p&gt;Sometimes you need to know if a UITextView is activly being edited. Here is a simple snippet to check if a UITextView is the first responder if it is then resign it. This will dismiss the keyboard and cancel editing of the UITextView.

&lt;pre class="brush:cpp"&gt;
if([myTextView isFirstResponder])[myTextView resignFirstResponder];
&lt;/pre&gt;&lt;/p&gt;</description><link>http://kwigbo.com/post/749715445</link><guid>http://kwigbo.com/post/749715445</guid><pubDate>Tue, 29 Jun 2010 10:03:00 -0400</pubDate><category>snippet</category></item><item><title>To iPhone 4 Or Not iPhone 4</title><description>&lt;p&gt;I am probably one of the only iPhone developers who doesn’t own an iPhone. That sounds pretty strange, but I have been using my 1st gen iPod touch pretty much exclusively for development. This puts a damper on things with the release of the iPhone 4 and the new iOS 4. Apple is not supporting the 1st gen iPod touch anymore, which means no upgrade. Now it’s time to buy a new device. The new features of the iPhone 4 (especially the camera) are very enticing but I don’t think I will get it. I am still holding out hope that they will put a decent camera in the iPod touch. So for now unless I win the lottery I think I will have to wait to see what the next gen iPod touches are like before I buy something.&lt;/p&gt;</description><link>http://kwigbo.com/post/688496983</link><guid>http://kwigbo.com/post/688496983</guid><pubDate>Fri, 11 Jun 2010 19:27:00 -0400</pubDate><category>thoughts</category></item><item><title>iPhone SDK UIWebView Alert</title><description>&lt;p&gt;I found myself needing a quick way to show a simple web page in my apps. I created this custom UIAlertView to do just that. Code after the jump.

&lt;!-- more --&gt;

&lt;pre class="brush:cpp"&gt;
@interface WebAlert : UIAlertView &lt;UIWebViewDelegate&gt;
{
	@private
	UIWebView *alertWeb;
	UIActivityIndicatorView *activityIndicator;
}

- (void) loadURL:(NSString *) url;

@end
&lt;/pre&gt;

&lt;pre class="brush:cpp"&gt;
#import "WebAlert.h"

@implementation WebAlert

- (void) loadURL:(NSString *) url
{
	alertWeb.hidden = YES;
	[alertWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
	{
        alertWeb = [[[UIWebView alloc] initWithFrame:CGRectZero] retain];
		alertWeb.delegate = self;
		[self addSubview:alertWeb];
    }
	
    return self;
}

// UIWebView Delegate 

- (void) webViewDidStartLoad:(UIWebView *) webView
{
	if(activityIndicator == nil)
	{
		activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] retain];
		[self addSubview:activityIndicator];
	}
	
	[activityIndicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
	[activityIndicator stopAnimating];
	
	[self performSelector:@selector(showAboutWebView) withObject:nil afterDelay:.2];
}

- (void) showAboutWebView
{
	alertWeb.hidden = NO;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked)
	{
        [[UIApplication sharedApplication] openURL:request.URL];
        return false;
    }
	
    return true;
}

- (void)setFrame:(CGRect)rect
{
	[super setFrame:CGRectMake(0, 0, rect.size.width, 275)];
	self.center = CGPointMake(480/2, 320/2);
}

- (void)layoutSubviews
{
	CGRect buttonFrame = CGRectMake(0, 0, 0, 0);
	
	for (UIView *view in self.subviews)
	{                 
		if ([[[view class] description] isEqualToString:@"UIThreePartButton"])
		{                         
			view.frame = CGRectMake(view.frame.origin.x, self.bounds.size.height - view.frame.size.height - 12, view.frame.size.width, view.frame.size.height);
			if(buttonFrame.size.width == 0) buttonFrame = view.frame;
		}
	}
	
	int webHeight = 160;
	
	alertWeb.frame = CGRectMake(buttonFrame.origin.x+1, buttonFrame.origin.y - (webHeight + 10), 260, webHeight);
	
	float activeX = (alertWeb.frame.origin.x + (alertWeb.frame.size.width/2)) - (activityIndicator.frame.size.width/2);
	float activeY = (alertWeb.frame.origin.y + (alertWeb.frame.size.height/2)) - (activityIndicator.frame.size.height/2);
	
	activityIndicator.frame = CGRectMake(activeX, activeY, activityIndicator.frame.size.width, activityIndicator.frame.size.height);
}

- (void) dealloc
{
	[activityIndicator release];
	
	alertWeb.delegate = nil;
	[alertWeb stopLoading];
	[alertWeb release];
	
	[super dealloc];
}

@end
&lt;/pre&gt;&lt;/p&gt;</description><link>http://kwigbo.com/post/687225809</link><guid>http://kwigbo.com/post/687225809</guid><pubDate>Fri, 11 Jun 2010 11:00:39 -0400</pubDate><category>snippet</category></item><item><title>iPhone SDK Random UIColor</title><description>&lt;p&gt;Sometimes when testing things out you just need to give something a random background color. Here is a snippet for generating a random UIColor.&lt;/p&gt;

&lt;a target="_blank" href="http://iphonedevelopment.blogspot.com/2008/10/little-color-in-your-life.html"&gt;Found At&lt;/a&gt;

&lt;pre class="brush:cpp"&gt;
// UIColor+Random.h
@interface UIColor (Random)

+ (UIColor *) randomColor;

@end
&lt;/pre&gt;
&lt;pre class="brush:cpp"&gt;
// UIColor+Random.m
#import "UIColor+Random.h"

@implementation UIColor (Random)

+ (UIColor *) randomColor
{
	CGFloat red =  (CGFloat)random()/(CGFloat)RAND_MAX;
	CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
	CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
	return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

@end

&lt;/pre&gt;</description><link>http://kwigbo.com/post/680994703</link><guid>http://kwigbo.com/post/680994703</guid><pubDate>Wed, 09 Jun 2010 15:29:34 -0400</pubDate><category>snippet</category></item><item><title>Artifice Open Source!</title><description>&lt;p&gt;I have decided to make my iPhone game Artifice open source. Due to family obligations I can no longer support this game. This work is licensed under a &lt;a target="_blank" href="http://creativecommons.org/licenses/by/3.0/"&gt;Creative Commons Attribution 3.0 Unported License&lt;/a&gt;. You may use this code in any way you want. All I ask is a credit for the work I did with a link to kwigbo.com.&lt;/p&gt;

&lt;h3&gt;Features&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Cocos2d / UIKit mixture&lt;/li&gt;
&lt;li&gt;OpenFeint integration&lt;/li&gt;
&lt;li&gt;Push Notifications via Urban Airship&lt;/li&gt;
&lt;li&gt;Online turn based multiplayer&lt;/li&gt;
&lt;li&gt;App Engine application backend&lt;/li&gt;
&lt;/ul&gt;
&lt;a href="http://kwigbo.com/artifice"&gt;Get the source code here!&lt;/a&gt;</description><link>http://kwigbo.com/post/680329612</link><guid>http://kwigbo.com/post/680329612</guid><pubDate>Wed, 09 Jun 2010 10:52:00 -0400</pubDate><category>artifice</category><category>source code</category></item><item><title>iPhone SDK Memory Management</title><description>&lt;p&gt;Here is a great article on memory management in Objective-C&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="http://iphonedevelopertips.com/objective-c/memory-management.html"&gt;Memory Management&lt;/a&gt;&lt;/p&gt;</description><link>http://kwigbo.com/post/677231093</link><guid>http://kwigbo.com/post/677231093</guid><pubDate>Tue, 08 Jun 2010 13:56:00 -0400</pubDate><category>link</category></item><item><title>iPhone SDK Get Path For File</title><description>&lt;p&gt;Here are two snippets that show how to get the path for a file in the documents directory or the main bundle.

&lt;pre class="brush:cpp"&gt;
// Main Bundle
NSString *pathInMainBundle = [[NSBundle mainBundle] pathForResource:@"example" ofType:@".png"];

// Documents Directory
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [documentPaths objectAtIndex:0];
NSString *pathInDocuments = [docsDir stringByAppendingPathComponent:@"example.png"];
&lt;/pre&gt;&lt;/p&gt;</description><link>http://kwigbo.com/post/676608391</link><guid>http://kwigbo.com/post/676608391</guid><pubDate>Tue, 08 Jun 2010 09:34:54 -0400</pubDate><category>snippet</category></item><item><title>Check If A File Exists</title><description>&lt;p&gt;This simple snippet use the NSFileManager to check if a file exists.&lt;/p&gt;

&lt;pre class="brush:cpp"&gt;
NSFileManager *fileManager = [[NSFileManager alloc] init];
BOOL exists = [fileManager fileExistsAtPath:@"Path To File"];
[fileManager release];
&lt;/pre&gt;</description><link>http://kwigbo.com/post/676532320</link><guid>http://kwigbo.com/post/676532320</guid><pubDate>Tue, 08 Jun 2010 09:00:00 -0400</pubDate><category>snippet</category></item><item><title>Convert Seconds To Minutes String</title><description>&lt;p&gt;&lt;pre class="brush:cpp"&gt;
- (NSString *) secondsToMinutesString:(int) seconds
{
	NSString *secondsStr;
	
	if(seconds % 60 &lt; 10) secondsStr = [@"0" stringByAppendingFormat:@"%d", seconds % 60];
	else secondsStr = [@"" stringByAppendingFormat:@"%d", seconds % 60];
	
	return [NSString stringWithFormat:@"%.0f : %@", floor(seconds/60), secondsStr];
}
&lt;/pre&gt;&lt;/p&gt;</description><link>http://kwigbo.com/post/631314889</link><guid>http://kwigbo.com/post/631314889</guid><pubDate>Tue, 25 May 2010 09:50:29 -0400</pubDate><category>snippet</category></item><item><title>Resize a UIImage the right way</title><description>&lt;p&gt;This is a great article about resizing a UIImage. It also includes some good insight into general iphone development as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/"&gt;http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/&lt;/a&gt;&lt;/p&gt;</description><link>http://kwigbo.com/post/556254929</link><guid>http://kwigbo.com/post/556254929</guid><pubDate>Wed, 28 Apr 2010 13:10:46 -0400</pubDate><category>link</category></item><item><title>Promote Your Game FREE!</title><description>&lt;p&gt;**Offer Expired**&lt;/p&gt;

&lt;p&gt;I am looking for a few good games to promote through the more games section in ABC123 and Artifice. ABC123 is scheduled to be the OpenFeint free game of the day (&lt;a href="http://freegameoftheday.com/"&gt;http://freegameoftheday.com/&lt;/a&gt;) on March 24th. Artifice is scheduled to be the free app of the day on freeappaday.com on April 6th. Both Artifice and ABC123 are cross promoted through the more games section in each game. Games in the more games section of both ABC123 and Artifice should see a decent amount of exposure during this time of heavy promotion. There are a limited amount of spots available and I am not charging anything for these spots. What I am looking for is high quality games. If you are interested please fill out the &lt;a href="http://kwigbo.com/contact"&gt;contact form&lt;/a&gt; and let me know which game you would like to submit for a possible spot.&lt;/p&gt;</description><link>http://kwigbo.com/post/464234169</link><guid>http://kwigbo.com/post/464234169</guid><pubDate>Sun, 21 Mar 2010 19:01:00 -0400</pubDate></item><item><title>Rectangle To Rectangle Collision Detection</title><description>&lt;p&gt;There is a built in method for testing collisions between rectangles.
&lt;pre class="brush:cpp"&gt;
BOOL colide = CGRectIntersectsRect(rect1, rect2);
&lt;/pre&gt;
Here is a method I wrote before I new the former had existed.
&lt;pre class="brush:cpp"&gt;
- (BOOL) rect:(CGRect) rect collisionWithRect:(CGRect) rectTwo
{
	float rect_x1 = rect.origin.x;
	float rect_x2 = rect_x1+rect.size.width;
	
	float rect_y1 = rect.origin.y;
	float rect_y2 = rect_y1+rect.size.height;
	
	float rect2_x1 = rectTwo.origin.x;
	float rect2_x2 = rect2_x1+rectTwo.size.width;
	
	float rect2_y1 = rectTwo.origin.y;
	float rect2_y2 = rect2_y1+rectTwo.size.height;
	
	if((rect_x2 &gt; rect2_x1 &amp;&amp; rect_x1 &lt; rect2_x2) &amp;&amp;(rect_y2 &gt; rect2_y1 &amp;&amp; rect_y1 &lt; rect2_y2))
		return YES;
	
	return NO;
}
&lt;/pre&gt;&lt;/p&gt;</description><link>http://kwigbo.com/post/426700865</link><guid>http://kwigbo.com/post/426700865</guid><pubDate>Thu, 04 Mar 2010 15:02:00 -0500</pubDate><category>snippet</category></item><item><title>Comparison Of XML Parsers For The iPhone SDK</title><description>&lt;p&gt;&lt;a href="http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project"&gt;http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project&lt;/a&gt;&lt;/p&gt;</description><link>http://kwigbo.com/post/426203901</link><guid>http://kwigbo.com/post/426203901</guid><pubDate>Thu, 04 Mar 2010 08:22:39 -0500</pubDate></item></channel></rss>
