Friday, January 13, 2012

UIButton with gradient

The following code adds a gradient effect to the button:

    CAGradientLayer* gradientLayer = [[CAGradientLayer alloc] init];
    [gradientLayer setBounds:[buttonTest bounds]];
    [gradientLayer setPosition:
     CGPointMake([buttonTest bounds].size.width/2,
                 [buttonTest bounds].size.height/2)];
    
    [gradientLayer setColors:
     [NSArray arrayWithObjects:
      (id)[[UIColor blueColor] CGColor],
      (id)[[UIColor cyanColor] CGColor], nil]];
    [[buttonTest layer] insertSublayer:gradientLayer atIndex:0];    
    [gradientLayer release];
The result may look so:

UIButton with a border

Let's say, you know how to add a button to your application.  Now let's add a border:

This effect should be done the code, for example in -viewDidLoad method:
- (void)viewDidLoad
{
    [super viewDidLoad];
    [[buttonTest layer] setCornerRadius:8.0f];
    [[buttonTest layer] setMasksToBounds:YES];
    [[buttonTest layer] setBorderWidth:3.0f];
}

UIButton. Change the background color in Interface Builder

It could be funny, if it was not sad - it was not very easy to change the button color in Interface Builder. :) The key is the button type - set it to "Custom":

Then, you can find where to change the background color:

Thursday, December 29, 2011

@synchronized

In case you need to synchronize: @synchronized, NSLock, pthread, OSSpinLock showdown, done right Occasionally found this article and I think it's very helpful - in extreamely short form, as working examples, it shows all synchronization tools.

Sunday, December 25, 2011

Read data from a file in the application bundle

Very simple:

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"Name" ofType:@"dat"];  
NSData* data = [NSData dataWithContentsOfFile:filePath]; 
Or, in case of a text data:
NSString* filePath = [[NSBundle mainBundlepathForResource:@"Name" ofType:@"txt"];  
NSString* text = [NSString stringWithContentsOfFile:filePath 
                                               encoding:NSUTF8StringEncoding 
                                                  error:nil];

Sunday, December 11, 2011

Android UI vs iPhone UI

I found an article talking about UI in Android and iPhone:

Why Android Will Always Be Laggier Than iOS The author says that UI tasks in Android is running in the main thread with the normal priority and only now got the hardware acceleration. The iPhone gives the high priority to the UI tasks. And it's good, etc.

Friday, December 2, 2011

Accelerometer. It's simple

The simplest way to use accelerometer in an iPhone application is UIAccelerometer class:
    UIAccelerometer* accelerometer = [UIAccelerometer sharedAccelerometer];
This code above shows how to get the accelerometer instance in the code.
The following line sets up the update interval:
    [accelerometer setUpdateInterval: 1.0 / 10.0f];
    [accelerometer setDelegate:self];
each 0.1 second the accelerometer will update the program that implements delegate method:
 - (void)accelerometer:(UIAccelerometer *)acel didAccelerate:(UIAcceleration *)aceler 
{
    NSLog(@"acceleration.x = %+.6f", aceler.x);
    NSLog(@"acceleration.y = %+.6f", aceler.y);
    NSLog(@"acceleration.z = %+.6f", aceler.z);
}
Do not forget to add UIAccelerometerDelegate, for example, to a view controller class:

@interface ViewController : UIViewController <UIAccelerometerDelegate>


So here is the step-by-step scenario for beginners:
1. Create new Single-View project in Xcode.
2. In ViewController.h file add <UIAccelerometerDelegate> after UIViewController.
3. In ViewController.m file, modify viewDidLoad method:
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIAccelerometer* accelerometer = [UIAccelerometer sharedAccelerometer];
    [accelerometer setUpdateInterval: 1.0 / 10.0f];
    [accelerometer setDelegate:self];
}
4. In ViewController.m file, add new method:

- (void)accelerometer:(UIAccelerometer *)acel didAccelerate:(UIAcceleration *)aceler 
{
    NSLog(@"acceleration.x = %+.6f", aceler.x);
    NSLog(@"acceleration.y = %+.6f", aceler.y);
    NSLog(@"acceleration.z = %+.6f", aceler.z);
}
This program works on iPhone. It does not make much sense to test it simulator.


Xcode 4: Add Existing Framework

1. Select your project in the project navigator - on the left side of the Xcode window.
2. On the right side of the window, choose the target.
3. Choose Summary page (press on the Summary button on the top of the window).
4. Scroll down till find "Linked Frameworks and Libraries".
5. Press on plus button to add an existing framework from the list:
iOS Developer Library. Linking To A Library Or Framework.