簡體   English   中英

多個動畫一個接一個

[英]Multiple animations after each other

我遇到了一個問題,我找不到任何解決方案。

所以基本上,我有一個指南針,我想“彈出”/“彈出”到視圖中,然后開始更新航向,但是在我關閉/彈出指南針后航向停止更新。

如:用戶想要指南針-> 按下按鈕-> 指南針彈出-> 開始旋轉指南針針-> 用戶不再需要指南針/關閉指南針-> 指南針彈出視圖。

所以我的問題是:如果我制作了一個動畫,另一個動畫停止了,我如何才能讓兩個動畫互相追趕?

在 showCompass 中:

(IBAction) showCompass:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
directionsArrow.center = CGPointMake(160, 410);
[UIView commitAnimations];

在位置管理器中:

float oldRadian = (-manager.heading.trueHeading * M_PI/180.0f);
float newRadian = (-newHeading.trueHeading * M_PI/180.0f);

CABasicAnimation *animation;
        animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.fromValue = [NSNumber numberWithFloat:oldRadian];
animation.toValue = [NSNumber numberWithFloat:newRadian];
animation.duration = 0.5f;

[directionsArrow.layer addAnimation:animation forKey:@"animateMyRotation"];
directionsArrow.transform = CGAffineTransformMakeRotation(newRadian);

在解雇指南針:

-(IBAction) dismissCompass {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
directionsArrow.center = CGPointMake(160, 410);
[UIView commitAnimations];
[locationManager stopUpdateHeading];
}

新代碼: *顯示: *

[UIView animateWithDuration:1.f
                 animations:^{
                     compassDial.center = CGPointMake(160, 504-92);
                     pushView.center = CGPointMake(160, 500-92);
                     //directionsArrow.center = CGPointMake(160, 502-92);
                 } completion:^(BOOL finished) {
                     directionsArrow.hidden = NO;
                     [locationManager startUpdatingHeading];
                 }];

在位置管理器中:

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
float oldRadian = (-manager.heading.trueHeading * M_PI/180.0f);
float newRadian = (-newHeading.trueHeading * M_PI/180.0f);

CABasicAnimation *animation;
        animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.fromValue = [NSNumber numberWithFloat:oldRadian];
animation.toValue = [NSNumber numberWithFloat:newRadian];
animation.duration = 0.5f;

[directionsArrow.layer addAnimation:animation forKey:@"animateMyRotation"];
directionsArrow.transform = CGAffineTransformMakeRotation(newRadian);

解雇:

    [UIView animateWithDuration:1.f
    animations:^{
                compassDial.center = CGPointMake(160, 700); 
                directionsArrow.center = CGPointMake(160, 700);

                 } completion:^(BOOL finished) {
                   [locationManager stopUpdatingHeading];
                 }];

提前致謝。

我假設您想要同時發生的兩個動畫是幀的旋轉和移動?

確保動畫流暢並符合您期望的最簡單的方法是將指南針放在另一個視圖中。

然后,您可以為指南針的父級框架設置動畫,使指南針移入和移出,並將旋轉僅應用於指南針本身。

展示

[UIView animateWithDuration:1.f
                 animations:^{
                   compassContainer.center = CGPointMake(160, 410);
                 }];

回轉

// The code you already have changing the transform of directionsArrow

解雇

[UIView animateWithDuration:1.f
                 animations:^{
                   compassContainer.center = CGPointMake(160, 410);
                 } completion:^(BOOL finished) {
                   [locationManager stopUpdateHeading];
                 }];

正如 Paul.s 在評論中提到的,這不是當前制作動畫的首選方式。 使用動畫的塊方法,您要問的問題真的很容易。 看看文檔,這里有一個簡短的例子。

-(IBAction)animateDelete:(id)sender
{
    [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         // Your first animation here...
                     }
                     completion:^(BOOL finished) {
                         // Do some stuff
                         [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
                                          animations:^{
                                              // Your second animation here...
                                          }
                                          completion:^(BOOL finished) {
                                              // Do some stuff
                                          }
                          ];
                     }
    ];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM