Objc – Digital clock using IBOutletCollection

Today, a simple snippet to learn how to use IBOutletCollection and image spritesin your applications.

IBOutletCollection are introduced with iOS4 as you can see on Apple iOS diff guide ( http://goo.gl/C8NRZz ) and are a collection of IBOutlets.

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]@interface APViewController : UIViewController
{
IBOutletCollection (UIView) NSArray *_digitViews;
NSTimer *_timer;
}[/code]

That you can connect in the classic way:
ib-guide

To read and use these connected views, loop the array and extract the UIView:

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]- (void)viewDidLoad
{
[super viewDidLoad];

UIImage *digits = [UIImage imageNamed:@"numbers.png"];
for ( UIView *view in _digitViews )
{
view.layer.contents = (__bridge  id)digits.CGImage;
view.layer.contentsRect = CGRectMake(0, 0, 0.1, 1.0);
view.layer.contentsGravity = kCAGravityResizeAspect;
}

_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(tick)
userInfo:nil repeats:YES];

[self tick];
}[/code]

Now, we add an image, this one:
numbers
click to open in a new window, and add to your project.

This is our sprite. Do you know what is a sprite, true?
http://en.wikipedia.org/wiki/Sprite_(computer_graphics) ]

Now, the view are connected, the image before is imported on your project, it’s time to code a little bit!

Load a number from the sprite:

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]- (void) setDigit:(NSInteger)digit forView:(UIView *)view
{
view.layer.contentsRect = CGRectMake(digit * 0.1, 0, 0.1, 1.0);
}[/code]

Set current time:

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]- (void) tick
{
// create a calendar with current time (hh:mm:ss)
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSUInteger units = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];

// load digit from sprite
[self setDigit:components.hour / 10 forView:_digitViews[0]];
[self setDigit:components.hour % 10 forView:_digitViews[1]];

[self setDigit:components.minute / 10 forView:_digitViews[2]];
[self setDigit:components.minute % 10 forView:_digitViews[3]];

[self setDigit:components.second / 10 forView:_digitViews[4]];
[self setDigit:components.second % 10 forView:_digitViews[5]];
}[/code]

Build and run!

Schermata 2013-10-22 alle 10.13.44

Ref:
– http://goo.gl/1TFH2I
– albertopasca.it

Alberto Pasca
Latest posts by Alberto Pasca (see all)