Normalerweise wird ein Ereignis, das nichts anderes als eine Nachricht festgelegt ist (gesetzt und umgesetzt) in der gleichen Klasse oder Rahmen, die Funktion oder Prozedur "Anrufer" ist. o in un UIViewController . Wenn wir zum Beispiel eine Schaltfläche hinzufügen UIButton via Code (programmatisch), können wir unsere Klasse in einem zu finden UIView oder einem UIViewController . In beiden Fällen ist die Aufgabenverteilung und die Initialisierung der Taste wird durch die Einstellung des befolgt werden target , die eine Nachricht erhalten soll, wenn es "Klick" auf die Schaltfläche ein:
1 2 3 4 5 6 7 8 9 | [ UIButton buttonWithType : UIButtonTypeRoundedRect ] ; UIButton Taste * = [UIButton buttonWithType: UIButtonTypeRoundedRect]; 10 , 180 , 300 , 30 ) ; bottone.frame = CGRectMake (10, 180, 300, 30); @ "Press me" forState : UIControlStateNormal ] ; [Button setTitle: @ "Drück mich" forState: UIControlStateNormal]; / / Entscheiden Sie, wer die Nachricht erhalten UIControlEventTouchUpInside self action : @selector ( onButtonClicked ) forControlEvents : UIControlEventTouchUpInside ] ; [AddTarget Button: Selbst Aktion: @ selector (OnButtonClicked) forControlEvents: UIControlEventTouchUpInside]; / / ... void ) onButtonClicked { - (Void) {OnButtonClicked / / ... } |
Zeile 5 der oben angezeigten Code entscheidet, wer (Subjekt) und was (Verfahren) "Anruf", wenn unsere Taste gedrückt wird. Im obigen Beispiel ist bekannt, dass die Druckeinstellung für die Nachricht an das Verfahren gesendet wird dargestellt onButtonClick umgesetzt unten, und dann einen Teil der gleichen Kontext (oder Klasse). potremmo inviare il nostro messaggio ad un qualsiasi altro oggetto, posto quindi al difuori del contesto in uso. Die erste Überlegung ist offensichtlich, dass wir tun können, ist dann, dass durch Veränderung der Parameter self und action können wir unsere Botschaft an jedes andere Objekt zu senden, dann auch außerhalb des Kontextes, in Gebrauch. : Hier ein Beispiel: Eine Klasse UIApplicationDelegate eine UIViewController :
1 2 3 4 5 | / / / / MyAppDelegate.m / / SplashScreenController alloc ] ; splashScreenController = [SplashScreenController alloc]; splashScreenController.view ] ; [Window addSubview: splashScreenController.view]; |
associata al UIViewController stesso: Die SplashScreenController macht eine Methode, mit der Sie die Animation erlaubt UIView mit verbundenen UIViewController gleiche:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | / / / / SplashScreenController.m / / void ) animateBackgroundDown { - (Void) {animateBackgroundDown nil context : nil ] ; [UIView beginAnimations: nil context: nil]; 0.75 ] ; [UIView setAnimationDuration: 0,75]; UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; self ] ; [UIView setAnimationDelegate: self]; @selector ( onAnimationFinished ) ] ; [UIView setAnimationDidStopSelector: @ selector (onAnimationFinished)]; CGRect ) { 0 , 480 , 320 , 480 } ; self.view.frame = (CGRect) {0, 480, 320, 480}; ; [UIView commitAnimations]; } / / ... void ) onAnimationFinished { - (Void) {onAnimationFinished "Animazione terminata" ) ; NSLog (@ "Animation beendet"); } |
. Der obige Code zeigt eine Methode innerhalb der Klasse definiert SplashScreenController Typ UIViewController . ) onAnimationFinished definito più sotto, facente parte sempre della classe SplashScreenController . Es tut nichts, aber animieren die UIView animieren ihn hin, und wenn die Animation abgeschlossen ist, rufen Sie (senden eine Nachricht an sich selbst - und damit self ) onAnimationFinished unten definiert, ist ein Teil der Klasse immer SplashScreenController . , non saremo informati della fine dell'animazione: Daraus folgt, dass in unserem myAppDelegate , wenn wir die Methode aufrufen animateBackgroundDown , werden wir nicht von dem Ende der Animation zu informieren:
1 2 3 4 | / / / / MyAppDelegate.m / / ; [SplashScreenController animateBackgroundDown]; |
Was wir wollen, stattdessen ist es, eine neue Version erstellen animateBackgroundDown als ihm zu sagen, wo um die Nachricht zu Ende Animation senden und welche Methode zu nennen. In der Praxis wollen wir sicherstellen, können Sie in unserer Klasse schreiben myAppDelegate :
1 2 3 4 5 6 7 8 | / / / / MyAppDelegate.m / / self selector : @selector ( onAnimationFinished ) ] ; [SplashScreenController animateBackgroundDown: self selector: @ selector (onAnimationFinished)]; / / ... void ) onAnimationFinished { - (Void) {onAnimationFinished "Animazione terminata" ) ; NSLog (@ "Animation beendet"); } |
, bensì in myAppDelegate . Dieses Mal das Verfahren onAnimationFinished nicht im UIViewController , aber in myAppDelegate . nel modo seguente: Dazu ändern Sie einfach die Methode animateBackgroundDown in UIViewController wie folgt:
1 2 3 4 5 6 7 8 9 10 11 12 | / / / / SplashScreenController.m / / void ) animateBackgroundDown : ( id ) target selector : ( SEL ) selector { - (Void) animateBackgroundDown: (id) Zielauswahlvorrichtung: (SEL) {Selektor nil context : nil ] ; [UIView beginAnimations: nil context: nil]; 0.75 ] ; [UIView setAnimationDuration: 0,75]; UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; target ] ; [UIView setAnimationDelegate: target]; selector ] ; [UIView setAnimationDidStopSelector: Selektor]; CGRect ) { 0 , 480 , 320 , 480 } ; self.view.frame = (CGRect) {0, 480, 320, 480}; ; [UIView commitAnimations]; } |
). Jetzt haben wir eine Methode, die den "Kontext" (akzeptiert target ) und die Methode aufgerufen werden ( selector ). a qualsiasi altro “oggetto” / classe in grado di riceverlo. Nun, wenn die Animation beendet, erscheint die Meldung AnimationDidStop geht an myAppDelegate andere "Objekt" / Klasse, die sie empfangen können.










Es sind keine Kommentare für diesen Beitrag
Hinterlasse einen Kommentar