10 extraits utiles pour Apple iPhone

Exécuter une méthode après n secondes

La famille entière performSelector est vraiment intéressant et peut être utile dans une multitude de cas. Son application la plus simple et la plus courante est la suivante:

1
2
3
4
5
@selector ( myMethod ) withObject : nil afterDelay : 3 ] ; [Self performSelector: @ selector (myMethod) withObject: afterDelay nul: 3];
/ /
void ) myMethod { - (Void) {myMethod
"Hello World!" ) ; NSLog (@ "Bonjour tout le monde!");
}

Cependant, considérer que le "timer" n'est pas exact. Cette procédure devrait donc être utilisée quand il n'est pas invité à une précision temporelle "significatif".

Récupérer la version de l'application

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

Oui, c'est vrai ou faux?

e TRUE sono in pratica la stessa edentica cosa: Va passer au peigne fin le Apple iPhone du noyau, vous pouvez vous rendre compte que YES , true et TRUE edentica sont essentiellement la même chose:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/ / Définition du OUI
# Définir OUI (bool) 1
# Définir NO (bool) 0

/ / Définition de la vraie
# Définir true 1
# Définir false 0

/ / Définition de TRUE
# Si! Défini (TRUE)
# Définir TRUE 1
# Endif

# Si! Défini (FAUX)
# Définir FALSE 0
# Endif

Au moins pour l'instant ...

Brouter

1
2
3
# Import <AudioToolbox/AudioToolbox.h>
/ /
; AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);

CGImageRef du pointeur d'une UIImage

1
2
3
4
[ UIImage imageNamed : @ "LittleHeart.png" ] ; UIImage * heart = [UIImage imageNamed: @ "LittleHeart.png"];
heart CGImage ] ; CGImageRef image = [CGImage coeur];
/ / L'image peut maintenant être "pixellisé" sur un CGContextRef
CGRect ) { 0 , 0 , 100 , 100 } , image ) ; CGContextDrawImage (c, (CGRect) {0, 0, 100, 100}, image);

Animations

1
2
3
4
5
nil context : NULL ] ; [UIView beginAnimations: contexte néant: NULL];
1.5 ] ; [UIView setAnimationDuration: 1,5];
UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
/ / ...
; [UIView commitAnimations];

NSLog

1
2
3
"NSString object %@ " , myString ) ; NSLog (@ "NSString objet% @", myString);
"Float: %f " , myFloat ) ; NSLog (@ "float:% f", myFloat);
"Integer: %i " , myInt ) ; NSLog (@ "Integer:% i", myInt);

Convertisseur RGB à UIColor

1
# Définir RGBA (r, g, b, a) [UIColor colorWithRed: r/255.0 vert: bleu g/255.0: b/255.0 alpha: a]

Transmission de paramètres à une NSTimer

Profitant du paramètre userInfo vous pouvez envoyer un pointeur vers un objet à la méthode invoquée par notre chronomètre.

1
2
3
4
5
6
7
8
9
10
11
12
13
scheduledTimerWithTimeInterval : 1 target : self selector : @selector ( timerMethod ) userInfo : objectPointer repeats : YES ] ; [ NSTimer scheduledTimerWithTimeInterval: 1 cible: self selector: @ selector (timerMethod) userInfo: répète objectPointer: YES];

/ / ...

void ) timerMethod : ( NSTimer * ) timer { - (Void) timerMethod: ( NSTimer *) timer {
/ / Récupérer le pointeur de mon objet
timer userInfo ] ; objectPointer = [timer userInfo];
/ / Ou
myMethod ] ; [[Timer userInfo] myMethod];
[ [ timer userInfo ] myProperty ] ; int a = [[timer userInfo] myProperty];
/ / Quel est le même
[ objectPointer myProperty ] ; int a = [objectPointer myProperty];
}

Temps d'exécution

Voici une façon simple de calculer court laps de temps nécessaire pour vérifier la vitesse d'exécution du code:

1
2
3
4
; CFAbsoluteTime initialTime CFAbsoluteTimeGetCurrent = ();
/ / ... Code
; CFAbsoluteTime finalTime CFAbsoluteTimeGetCurrent = ();
"Tempo trascorso %f" , finalTime - initialTime ) ; NSLog (@ "Temps écoulé% f", finalTime - initialTime);

Il n'y a aucun commentaire pour cet article

Laisser un commentaire

TAG XHTML Permis: Entrée du code:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL