Samantha John's Blog

  • Archive
  • RSS

Cocos2d + UIKit

I haven’t been writing much in here because I’ve been busy programming our new Hopscotch iOS app. This is a little bit of a PSA to save others from the giant gotcha I encountered when integrating Cocos2d into my UIKit app to power user-generated animations. 

First, I used the excellent Kobold2d library to install cocos2d by following this tutorial. (This involved starting a new Xcode project and copying over my old files, amazing that this was far and away better than any alternative solution I encountered). Here’s where a whole new set of problems kicked int: my app became incredibly slow. It was difficult to pinpoint the problem since so many things had to change in order to integrate this library.

My original code to integrate cocos2d looked something like this: 

https://gist.github.com/3661164

After several weeks of forging ahead and continually wondering why my app was so crashy and slow I finally sat down to solve the problem. I used Apple’s Instruments application with the core animation template (NB: this only works on your device, not the simulator). When I checked “Flash Updated Regions” it quickly became clear what was wrong: my (mostly) offscreen stage view where animations would render was updating 60 times per second, even when all animations were stopped. 

Armed with this knowledge the solution was simple. I set the frame right down to 10 seconds and only increased it when an animation was actually running. I added code to the reveal and conceal functions to make sure the frame right was always appropriate and my app was back to its old responsive self: 

https://gist.github.com/3661121
    • #ios
    • #cocos2d
    • #kobald2d
    • #objective-c
    • #core animation
    • #performance
  • 8 months ago
  • Comments
  • Permalink
  • Share
    Tweet

iOS programming: calculate height of text area

EDIT: A friend on Twitter informed me that the better way to do this is: 

[nameText sizeWithFont:font constrainedToSize:CGSizeMake(300.f, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

The key being that you can create a CGSize object with MAXFLOAT as the height. 

My iOS skills are certainly a work in progress. 

While learning objective C I thought a good test project would be to give some love to my old friend Nerd Nearby. 

Nerd nearby is pulling local info from several services such as instagram and foursquare. Since the data is unique to each user we have to dynamically size each section of each cell to accomodate different amounts of text.

A sneak peak at the new app

In this example there are 2 lines of text but this will change with each item. This is trivially easy in HTML/CSS but it turns out that in iOS it’s tricky to do dynamic resizing because here are no built in functions to find out how tall your text should be given a fixed height.

My solution was to add a category that extended NSString. In objective C they make it really really easy to add methods to a class from any library. This is a little scary to me but for now I’m rolling with it. Here’s my code:

In NSString+NerdNearby.h: 

@interface NSString (NerdNearby)
- (CGSize) multilineSizeWithFont:(UIFont *)font forWidth:(float)width lineBreakMode:(UILineBreakMode)lineBreakMode;
@end

And the gross bit, in NSString+NerdNearby.m:

@implementation NSString (NerdNearby)
- (CGSize) multilineSizeWithFont:(UIFont *)font forWidth:(float)width lineBreakMode:(UILineBreakMode)lineBreakMode
{
   CGSize size = [self sizeWithFont:font];
   CGSize oneLineSize = [self sizeWithFont:font forWidth:width lineBreakMode:lineBreakMode];

   if (size.width > width) {
     float area = size.width * size.height;
     float newHeight = area/widthSize.width;
     if ((int)ceil(newHeight) % (int)oneLineSize.height != 0 ) {
       newHeight = (float) ((ceil)(newHeight/oneLineSize.height) * (int)oneLineSize.height);
     }
     size.height = newHeight;
   }
   size.width = width;
   return size;
}

@end

This is certainly not perfect. For instance, it only checks if the first line break would be shorter than the width. I’m not really sure how to access how long the second line of text would be. For my purposes though, it’s good enough. 

Read More

    • #iOS
    • #objective-C
    • #objective c
    • #nsstring
    • #categories
    • #sizeWithFont
  • 1 year ago
  • 3
  • Comments
  • Permalink
  • Share
    Tweet

About

Avatar Entrepreneur and founder of Hopscotch. These are my thoughts on programming and life.

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Mobile

Effector Theme by Pixel Union.

Powered by Tumblr