Domanda

Quali sono le best practice considerate per l'animazione delle transizioni delle visualizzazioni su iPhone?

Ad esempio, il progetto di esempio ViewTransitions di apple utilizza un codice simile a:

CATransition *applicationLoadViewIn = [CATransition animation];
[applicationLoadViewIn setDuration:1];
[applicationLoadViewIn setType:kCATransitionReveal];
[applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[[myview layer] addAnimation:applicationLoadViewIn forKey:kCATransitionReveal];

ma ci sono anche frammenti di codice che fluttuano nella rete in questo modo:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myview cache:YES];
[myview removeFromSuperview];
[UIView commitAnimations];

Qual è l'approccio migliore? Se anche tu potessi fornire uno snippet, sarebbe molto apprezzato.

NOTA: non sono riuscito a far funzionare correttamente il secondo approccio.

È stato utile?

Soluzione

Dal riferimento UIView sulla sezione beginAnimations: context: :

  

L'uso di questo metodo è sconsigliato in iPhone OS 4.0 e versioni successive. Dovresti invece utilizzare i metodi di animazione basati su blocchi.

Ad esempio di animazione basata su blocchi basata sul commento di Tom

[UIView transitionWithView:mysuperview 
                  duration:0.75
                   options:UIViewAnimationTransitionFlipFromRight
                animations:^{ 
                    [myview removeFromSuperview]; 
                } 
                completion:nil];

Altri suggerimenti

Ho usato quest'ultimo per molte animazioni leggere e carine. Puoi usarlo con dissolvenza incrociata di due viste, oppure dissolverne uno di fronte a un altro o sfumarlo. Puoi girare una vista su un'altra come uno striscione, puoi allungare o restringere una vista ... Sto ottenendo molta distanza in miglia da beginAnimation / commitAnimations .

Non pensare che tutto ciò che puoi fare sia:

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myview cache:YES];

Ecco un esempio:

[UIView beginAnimations:nil context:NULL]; {
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelegate:self];
    if (movingViewIn) {
// after the animation is over, call afterAnimationProceedWithGame
//  to start the game
        [UIView setAnimationDidStopSelector:@selector(afterAnimationProceedWithGame)];

//      [UIView setAnimationRepeatCount:5.0]; // don't forget you can repeat an animation
//      [UIView setAnimationDelay:0.50];
//      [UIView setAnimationRepeatAutoreverses:YES];

        gameView.alpha = 1.0;
        topGameView.alpha = 1.0;
        viewrect1.origin.y = selfrect.size.height - (viewrect1.size.height);
        viewrect2.origin.y = -20;

        topGameView.alpha = 1.0;
    }
    else {
    // call putBackStatusBar after animation to restore the state after this animation
        [UIView setAnimationDidStopSelector:@selector(putBackStatusBar)];
        gameView.alpha = 0.0;
        topGameView.alpha = 0.0;
    }
    [gameView setFrame:viewrect1];
    [topGameView setFrame:viewrect2];

} [UIView commitAnimations];

Come puoi vedere, puoi giocare con alfa, cornici e persino dimensioni di una vista. Divertirsi. Potresti essere sorpreso dalle sue capacità.

La differenza sembra essere la quantità di controllo di cui hai bisogno sull'animazione.

L'approccio CATransition ti dà più controllo e quindi più cose da impostare, ad es. la funzione di temporizzazione. Essendo un oggetto, puoi memorizzarlo per dopo, refactor per puntare tutte le tue animazioni su di esso per ridurre il codice duplicato, ecc.

I metodi di classe UIView sono metodi convenienti per animazioni comuni, ma sono più limitati di CATransition . Ad esempio, ci sono solo quattro possibili tipi di transizione (capovolgi a sinistra, capovolgi a destra, rannicchiarsi, rannicchiarsi). Se si desidera eseguire una dissolvenza in apertura, è necessario scavare fino alla transizione di dissolvenza di CATransition o impostare un'animazione esplicita dell'alfa del UIView .

Nota che CATransition su Mac OS X ti permetterà di specificare un filtro CoreImage arbitrario da usare come transizione, ma allo stato attuale non puoi farlo su l'iPhone, che manca di CoreImage .

Possiamo animare le immagini in iOS 5 usando questo semplice codice.

CGRect imageFrame = imageView.frame;
imageFrame.origin.y = self.view.bounds.size.height;

[UIView animateWithDuration:0.5
    delay:1.0
    options: UIViewAnimationCurveEaseOut
    animations:^{
        imageView.frame = imageFrame;
    } 
    completion:^(BOOL finished){
        NSLog(@"Done!");
    }];

Nei documenti UIView , leggi questa funzione per ios4+

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion

Ad ogni modo il " Block " il metodo è preferito oggi. Spiegherò il semplice blocco di seguito.

Considera il frammento di seguito. bug2 e bug 3 sono imageViews. L'animazione di seguito descrive un'animazione della durata di 1 secondo dopo un ritardo di 1 secondo. Il bug3 viene spostato dal suo centro al centro del bug2. Una volta completata, l'animazione verrà registrata " Center Animation Done! & Quot ;.

-(void)centerAnimation:(id)sender
{
NSLog(@"Center animation triggered!");
CGPoint bug2Center = bug2.center;

[UIView animateWithDuration:1
                      delay:1.0
                    options: UIViewAnimationCurveEaseOut
                 animations:^{
                     bug3.center = bug2Center;
                 } 
                 completion:^(BOOL finished){
                     NSLog(@"Center Animation Done!");
                 }];
}

Spero sia pulito !!!

Ho trovato un buon tutorial in questo link. Spero che questo possa essere utile per qualcuno.

uiview-animation-tutorial

Ecco il codice per animazioni fluide, potrebbe essere utile per molti sviluppatori.
Ho trovato questo frammento di codice in questo tutorial.

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[animation setAutoreverses:YES];
[animation setFromValue:[NSNumber numberWithFloat:1.3f]];
[animation setToValue:[NSNumber numberWithFloat:1.f]];
[animation setDuration:2.f];
[animation setRemovedOnCompletion:NO];

[animation setFillMode:kCAFillModeForwards];
[[self.myView layer] addAnimation:animation forKey:@"scale"];/// add here any Controller that you want t put Smooth animation.

proviamo a fare il checkout per Swift 3 ...

UIView.transition(with: mysuperview, duration: 0.75, options:UIViewAnimationOptions.transitionFlipFromRight , animations: {
    myview.removeFromSuperview()
}, completion: nil)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top