iPhone UIView Animation Best Practice

Jaka jest najlepsza praktyka animowania przejść widoku na iPhonie?

Przykładowy projekt ViewTransitions firmy apple używa kodu w stylu:

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

Ale są też urywki kodu unoszące się po sieci, które wyglądają tak:

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

Jakie jest najlepsze podejście? Gdybyś mógł podać fragment, byłoby to bardzo mile widziane.

Uwaga: nie udało mi się uzyskać drugiego podejścia do poprawnego działania.

Author: Bartłomiej Semańczyk, 2009-03-10

9 answers

Z sekcji UIView reference o metodzie beginAnimations:context::

[[2]}stosowanie tej metody jest zalecane w iPhone OS 4.0 i nowszych. Zamiast tego należy użyć metod animacji opartych na blokach.

Np. animacji opartej na bloku na podstawie komentarza Toma

[UIView transitionWithView:mysuperview 
                  duration:0.75
                   options:UIViewAnimationTransitionFlipFromRight
                animations:^{ 
                    [myview removeFromSuperview]; 
                } 
                completion:nil];
 118
Author: Rafael Vega,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-18 20:03:39

Używałem tego ostatniego do wielu ładnych, lekkich animacji. Możesz go użyć, tworząc dwa widoki, lub zaciemniając jeden przed drugim lub zaciemniając go. Można strzelać widok na inny jak baner, można zrobić widok rozciągnąć lub zmniejszyć... Dostaję dużo przebiegu z beginAnimation/commitAnimations.

Nie myśl, że wszystko co możesz zrobić to:

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

Oto przykład:

[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];

Jak widzisz, możesz grać alfą, klatkami, a nawet rozmiarami widoku. Pobaw się. Możesz być zaskoczony jego możliwościami.

 69
Author: mahboudz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-29 06:43:21

Wydaje się, że różnica polega na ilości kontroli nad animacją.

Podejście CATransition daje większą kontrolę i tym samym więcej rzeczy do skonfigurowania, np. funkcja pomiaru czasu. Będąc obiektem, możesz go zapisać na później, refaktor, aby skierować na niego wszystkie animacje, aby zmniejszyć zduplikowany kod itp.

Metody klasy UIView są wygodnymi metodami dla popularnych animacji, ale są bardziej ograniczone niż CATransition. Na przykład, istnieją tylko cztery możliwe typy przejścia (Odwróć w lewo, Odwróć w prawo, zwiń w górę, zwiń w dół). Jeśli chcesz wykonać fade in, musisz albo przekopać się do CATransition's fade transition, albo skonfigurować wyraźną animację Alfy UIView.

Zauważ, że CATransition Na Mac OS X pozwoli Ci określić dowolny filtr CoreImage do użycia jako przejście, ale w obecnym stanie nie możesz tego zrobić na iPhonie, który nie ma CoreImage.

 52
Author: Ryan McCuaig,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-09 05:50:02

Możemy animować obrazy w systemie ios 5 za pomocą tego prostego kodu.

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!");
    }];
 26
Author: Guru,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-20 09:10:50

W UIView docs, przeczytaj o tej funkcji dla ios4+

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion
 8
Author: Chris,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-29 06:43:50

W każdym razie metoda "Block" jest teraz prefered-a-days. Wyjaśnię prosty blok poniżej.

Rozważ poniższy fragment. bug2 i bug 3 to imageViews. Poniższa animacja opisuje animację trwającą 1 sekundę po 1 sekundzie opóźnienia. Bug3 jest przenoszony ze swojego centrum do centrum bug2. Po zakończeniu animacji zostanie zapisana "Center Animation Done!".

-(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!");
                 }];
}
Mam nadzieję, że to czyste!!!
 6
Author: Deepukjayan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-28 10:12:40

Znalazłem dobry tutorial w tym linku. Mam nadzieję, że to będzie pomocne dla kogoś.

Uiview-animation-tutorial

 4
Author: Dilip Rajkumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-14 11:53:23

Oto kod do płynnej animacji, może być pomocny dla wielu programistów.
znalazłem ten fragment kodu z tego samouczka.

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.
 2
Author: iPatel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-03 08:39:17

Spróbujmy dokonać kasy na Swift 3...

UIView.transition(with: mysuperview, duration: 0.75, options:UIViewAnimationOptions.transitionFlipFromRight , animations: {
    myview.removeFromSuperview()
}, completion: nil)
 2
Author: Saurabh Sharma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-31 13:22:02