10 utili snippet per Apple iPhone

venerdì 8 gennaio, 2010

Eseguire un metodo dopo n secondi

Tutta la famiglia performSelector è davvero interessante e può essere utile in una moltitudine di casi. La sua applicazione più semplice e comune è la seguente:

Objective-C:
  1. [self performSelector:@selector(myMethod) withObject:nil afterDelay:3];
  2. //
  3. - (void)myMethod {
  4.     NSLog(@"Hello World!");
  5. }

Tuttavia considerate che il "timer" non è preciso. Questa procedura, quindi, va usata quando non è richiesta una "notevole" precisione temporale.

Recuperare la versione dell'applicazione

Objective-C:
  1. NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
  2. NSLog(@"versione = %@", version);

YES, true o TRUE?

Andando a spulciare nel Kernel Apple iPhone è possibile renderci conto che YES, true e TRUE sono in pratica la stessa edentica cosa:

Objective-C:
  1. // definizione di YES
  2. #define YES (BOOL)1
  3. #define NO  (BOOL)0
  4.  
  5. // definizione di true
  6. #define true  1
  7. #define false 0
  8.  
  9. // definizione di TRUE
  10. #if !defined(TRUE)
  11.     #define TRUE 1
  12. #endif
  13.  
  14. #if !defined(FALSE)
  15.     #define FALSE 0
  16. #endif

Almeno per adesso...

Vibrazione

Objective-C:
  1. #import <AudioToolbox/AudioToolbox.h>
  2. //
  3. AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);

Puntatore CGImageRef a partire da un UIImage

Objective-C:
  1. UIImage *heart = [UIImage imageNamed:@"LittleHeart.png"];
  2. CGImageRef image = [heart CGImage];
  3. // L'immagine adesso può essere "rasterizzata" su un CGContextRef
  4. CGContextDrawImage(c, (CGRect){0, 0, 100, 100}, image);

Animazioni

Objective-C:
  1. [UIView beginAnimations:nil context:NULL];
  2. [UIView setAnimationDuration:1.5];
  3. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  4. // ...
  5. [UIView commitAnimations];

NSLog

Objective-C:
  1. NSLog(@"NSString object %@ ", myString);
  2. NSLog(@"Float: %f ", myFloat);
  3. NSLog(@"Integer: %i ", myInt);

Convertitore da RGB a UIColor

Objective-C:
  1. #define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]

Passare parametri ad un NSTimer

Sfruttando il parametro userInfo è possibile inviare un puntatore ad un nostro oggetto al metodo richiamato da timer.

Objective-C:
  1. [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod) userInfo:objectPointer repeats:YES];
  2.  
  3. // ...
  4.  
  5. -(void)timerMethod:(NSTimer*)timer {
  6. // Recupero il puntatore al mio oggetto
  7. objectPointer = [timer userInfo];
  8. // oppure
  9. [[timer userInfo] myMethod];
  10. int a = [[timer userInfo] myProperty];
  11. // che è lo stesso
  12. int a = [objectPointer myProperty];
  13. }

Tempo di esecuzione

Ecco un semplice modo per calcolare tempi brevi utili per verificare la velocità di esecuzione del codice:

Objective-C:
  1. CFAbsoluteTime initialTime = CFAbsoluteTimeGetCurrent();
  2. // ... code
  3. CFAbsoluteTime finalTime = CFAbsoluteTimeGetCurrent();
  4. NSLog(@"Tempo trascorso %f", finalTime-initialTime);

Post correlati

Questo articolo ti è stato utile?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

Non ci sono commenti per questo Post

Lascia un commento

TAG XHTML PERMESSI: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERIMENTO CODICE:
<pre></pre>         // blocco generico
[code][/code]       // blocco generico
[as][/as]           // Actionscript
[css][/css]         // CSS Style Sheet
[html][/html]       // HTML
[js][/js]           // Javascript
[objc][/objc]       // Objective-C
[php][/php]         // PHP
[sql][/sql]         // SQL