• iSENDu

    • iSENDu Videos
  • S-Cube

    • S-Cube Project
  • Event Seeker

    • volume.at
  • q·.:World

    • q·.:LAUNCHER
    • q·.:CARD
    • q·.:Generator
    • q·.:Specs
  • Home

    • Imprint
    • GTC
    • Sitemap
  • services
  • blog
  • help
  • about

ikangai.com

  • Search Blog

  • Pages

    • A Cross-Platform Software System to Create and Deploy Mobile Mashups
    • Apps using q·.:Codes
    • Concours Worldvision de la q·.:Launcher
    • expressFLOW
    • Prototype
  • Latest Entries

    • Writing Scientific Papers
    • Code Snippet of the Week: Using Quartz to draw the background of UIButtons
    • Concours Worldvision de la q·.:Launcher
    • Concours Eurovision de la Chanson
    • We’ve got mail from Apple
  • Categories

    • Code snippet (1)
    • Design (11)
    • Development (32)
    • Events (7)
    • Hardware (2)
    • iSENDu (39)
    • iTunes App Store (20)
    • News (13)
    • Science (7)
    • Software (18)
    • Sports (1)
    • Uncategorized (48)
    • University (2)
    • Website (4)
  • Archives

    • August 2010 (2)
    • July 2010 (7)
    • June 2010 (7)
    • May 2010 (6)
    • April 2010 (13)
    • March 2010 (11)
    • February 2010 (8)
    • January 2010 (8)
    • December 2009 (9)
    • November 2009 (6)
    • October 2009 (8)
    • September 2009 (13)
    • August 2009 (14)
  • Subscribe

    • Subscribe rss feed

Archive for the ‘Development’ Category

Code Snippet of the Week: Using Quartz to draw the...

Wednesday, August 11th, 2010

Ever wanted to use UIButtons with backgrounds that change when a UIButton is pressed? Unfortunately, this is not so straightforward as one could wish for. Typically, you have to use predefined images to do this、 and set the background image according the state of the UIButton with
[contactButton setBackgroundImage: imageHighlighted forState:UIControlStateHighlighted];
A standard solution that includes that a PNG UIImage is displayed when the button is pressed looks like this:

UIImage *imageHighlighted = [UIImage imageNamed:@"highlighted.png"];
UIButton *contactButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 158, 30)];
contactButton.backgroundColor = [UIColor clearColor];
[contactButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[contactButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
[contactButton setTitle:@"Contacts" forState:UIControlStateNormal];
[contactButton setTitle:@"Contacts" forState:UIControlStateHighlighted];
[contactButton actionsForTarget:@selector(selectContacts:) forControlEvent:UIControlEventTouchUpInside];
contactButton.titleLabel.font = [UIFont fontWithName:@"Verdana" size:12];
[contactButton setBackgroundColor:[UIColor clearColor]];
[contactButton setBackgroundImage: imageHighlighted forState:UIControlStateHighlighted];
[self addSubview:contactButton];

Wouldn’t it be nice to do this without PNG images you have to draw upfront? Fortunately, there is Quartz to help you with that. The main benefit of Quartz is its flexibility: you can apply all kind of effects on the UIButtons without needing to draw PNG images before. The Code for creating Quartz background images for UIButtons is straightforward: you have to define a context with UIGraphicsBeginImageContext from which you retrieve a UIImage with UIGraphicsGetImageFromCurrentImageContext. This image is later used in the button when it is highlighted: [contactButton setBackgroundImage: imageView.image forState:UIControlStateHighlighted];

// 1. create UIImageView container that contains a "nil" UIImage and assign variable for later use
UIImageView *imageView = [[UIImageView alloc] initWithImage:nil];
UIImage *image = imageView.image;
// 2. set size of the UIImageView -> the same as the UIButton
imageView.frame = CGRectMake(0, 0, 158, 30);
// 3. start image context
UIGraphicsBeginImageContext(CGSizeMake(158, 30));
// 4. define fill color of rectangle
CGContextSetFillColor(UIGraphicsGetCurrentContext(), CGColorGetComponents([UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor));
// 5. draw rectangle
[image drawInRect:CGRectMake(0, 0, 158, 30)];
// 6. create context path
CGContextBeginPath(UIGraphicsGetCurrentContext());
// 7. set origin
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 0, 0);
// 8. add rectanlge to path
CGContextAddRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, 158, 30));
// 9. fill rectangle
CGContextFillPath(UIGraphicsGetCurrentContext());
// 10. get image from current context
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

The trick is to use a UIImageView with an empty image: [[UIImageView alloc] initWithImage:nil]; This allows us later to access the UIImage from the UIImageView. The reason for this is that you cannot create an empty UIImage (at least, I’m not aware of this). The use is simple: you create a UIButton as you are used to and set the image for the state: [contactButton setBackgroundImage: image forState:UIControlStateHighlighted];

// 11. create UIButton
UIButton *contactButton = [[UIButton alloc]initWithFrame:CGRectMake(84, 225, 158, 30)];
contactButton.backgroundColor = [UIColor greenColor];
[contactButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[contactButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
[contactButton setTitle:@"Press Me" forState:UIControlStateNormal];
[contactButton setTitle:@"Color Change" forState:UIControlStateHighlighted];
contactButton.titleLabel.font = [UIFont fontWithName:@"Verdana" size:12];
// 12. set background image for highlighted state
[contactButton setBackgroundImage:image forState:UIControlStateHighlighted];
[self.view addSubview:contactButton];
[imageView release];
[contactButton release];

You can download the demo project from here.

Your coding ikangai team

PS: The code snippet uses portions of this code.

Tags: Background, Code Snippet of the Week, Quartz, UIButton
Posted in Code snippet, Development, Software | No Comments »

Apple Developer Center Overhaul

Friday, March 5th, 2010

Apple updated the developer center and streamlined the developer subscriptions. The name has changed to member center and there is a single mac developer subscription (79€ per year) that replaces previous membership subscriptions. Developers are required to agree to new terms of use and must complete a lengthy survey.

Your ikangai team

Tags: Apple, Developer Center
Posted in Development, News | No Comments »

Code Snippet of the week – Modal UIAlertView...

Friday, January 29th, 2010

A modal UIAlertView can be implemented completely differently than we did in our code snippet of the week. Erica Sadun’s iPhone cookbook provides an elegant solution for this problem. In a nutshell, the idea is to utilize the runloop as shown in the code snippet below.

#import "ModalAlert.h"
@interface ModalAlertDelegate : NSObject uialertviewdelegate
{
CFRunLoopRef currentLoop;
NSUInteger index;
}
@property (readonly) NSUInteger index;
@end
@implementation ModalAlertDelegate
@synthesize index;
-(id) initWithRunLoop: (CFRunLoopRef)runLoop
{
if (self = [super init]) currentLoop = runLoop;
return self;
}
-(void) alertView: (UIAlertView*)aView clickedButtonAtIndex: (NSInteger)anIndex
{
index = anIndex;
CFRunLoopStop(currentLoop);
}
@end
@implementation ModalAlert
+(NSUInteger) queryWith: (NSString *)question button1: (NSString *)button1 button2: (NSString *)button2
{
CFRunLoopRef currentLoop = CFRunLoopGetCurrent();
ModalAlertDelegate *madelegate = [[ModalAlertDelegate alloc] initWithRunLoop:currentLoop];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:question message:nil delegate:madelegate cancelButtonTitle:button1 otherButtonTitles:button2, nil];
[alertView show];
CFRunLoopRun();
NSUInteger answer = madelegate.index;
[alertView release];
[madelegate release];
return answer;
}
+ (BOOL) ask: (NSString *) question
{
return ([ModalAlert queryWith:question button1: @"Yes" button2: @"No"] == 0);
}
+ (BOOL) confirm: (NSString *) statement
{
return [ModalAlert queryWith:statement button1: @"Cancel" button2: @"OK"];
}
@end

your coding ikangai team

Tags: Code snippet, Modal, UIAlertView
Posted in Development, News | No Comments »

Code snippet of the week-Modal UIAlertView

Monday, January 25th, 2010

Some things on the iPhone are more complicated than they need to be. One example is the use of modal UIAlertViews to ask the user for feedback before continuing with the execution of the program. In a pseudo code notation, it looks like this:

startWithSomething()
int res = Inputbox("Do you want to save the data?", Yes, No)
if (res==1) {
  doSomething()
} else {
  doSomethingElse()
}
continueWithSomething()

This pseudo code snippet would wait until the user either decides Yes and then execute doSomething() followed by continueWithSomething() or doSomethingElse() followed by continueWithSomething().
If one decides to implement the same on the iPhone, we need to do a few additional things. First of all, we need to create an UIAlertView delegate that implements the UIAlertViewDelegate protocol. Then, we need to assure that the delegate informs us about the user decision. We do this by generating events to which we attach event observers:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething:) name:@"UserSaysYes" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomethingElse:) name:@"UserSaysNo" object:nil];

This code listens for events with the name UserSaysYes (UserSaysNo) and calls the method doSomething (doSomethingElse) if the corresponding event can be observed.
In the UIAlertViewDelegate we need to generate the events by adding the following code:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex=0) {
NSMutableArray *keys = [NSMutableArray arrayWithObjects:@"Info", nil];
NSMutableArray *objects = [NSMutableArray arrayWithObjects:@"Yes Sir!" , nil];
NSDictionary *info = [[NSDictionary dictionaryWithObjects:objects forKeys:keys]];
[[NSNotificationCenter defaultCenter]postNotificationName:@"UserSaysYes" object:info];
} else {
NSMutableArray *keys = [NSMutableArray arrayWithObjects:@"Info", nil];
NSMutableArray *objects = [NSMutableArray arrayWithObjects:@"No Sir!" , nil];
NSDictionary *info = [[NSDictionary dictionaryWithObjects:objects forKeys:keys]];
[[NSNotificationCenter defaultCenter]postNotificationName:@"UserSaysNo" object:info];
}

The implementation of doSomething (doSomethingElse) is straightforward: we parse the notification object and print the content on the console. Then we call continueWithSomething in order to complete our example.

-(void) doSomething:(NSNotification *) notification {
NSDictionary *info = [notification object];
NSstring* text = [info objectForKey:@"Info"];
NSLog(text);
[self continueWithSomething];
}
-(void) doSomethingElse:(NSNotification *) notification {
NSDictionary *info = [notification object];
NSstring* text = [info objectForKey:@"Info"];
NSLog(text);
[self continueWithSomething];
}

The drawback of this method is obviously that the modal logic is scattered among different method implementations which can make a program hard to read. However, if you have one or two places in your code that need modal user input, this is certainly a way to do it.

Your ikangai team

Tags: Code Snippet of the Week, Modal, UIAlertView
Posted in Development, Software | No Comments »

Memory Management on the iPhone

Sunday, January 10th, 2010

The Objective C language bears a few hurdles that new developers need to overcome. When browsing blogs and forums, there are a lot of posts concerned with memory management. A good point to start where to look is obvious Apple’s developer forum, but there are also other pages on the Web, which provide useful information.

A very simple guide that explains the use of NSZombieEnabled can be found here – thanks to the guys from paper bag for that link. Another, broader collection of tips can be found here.

Your ikangai team

Tags: iPhone, Memory Management
Posted in Development | No Comments »

Older Entries
Copyright © 2010 IKANGAI Solutions. Design by creativesyntax.at
IKANGAI Blog is powered by Wordpress | Login
Facebook It! Digg It! Stumble It! del.icio.us