【问题标题】:Multiple animations after each other多个动画相继出现
【发布时间】:2012-10-31 10:57:17
【问题描述】:

我遇到了一个问题,我找不到任何解决方案。

所以基本上,我有一个罗盘,我想“弹出”/“弹出”到视图中,然后开始更新航向,但在我关闭/弹出罗盘后航向停止更新。

喜欢:用户想要指南针-> 按下按钮-> 指南针弹出-> 开始旋转指南针针-> 用户不再想要指南针/关闭指南针-> 指南针弹出视图。

所以我的问题是:如果我制作了一个动画,另一个动画停止了,我如何让两者能够相互运行?

在showCompass中:

(IBAction) showCompass:

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

在 locationManager 中:

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);

在dismissCompass中:

-(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];
                 }];

在 locationManager 中:

- (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];
                 }];

提前致谢。

【问题讨论】:

  • 您可能想在动画完成后调用[locationManager stopUpdateHeading];... 还谈到用于动画的语法 - “在 iOS 4.0 及更高版本中不鼓励使用此方法。您应该使用使用基于块的动画方法来指定您的动画。”
  • 有什么问题?您发表了一堆声明并发布了一些代码,但从未说出实际问题是什么。
  • 问题是:如果我制作一个动画,另一个动画停止,我怎样才能让两个动画相互运行?

标签: iphone ios animation uiimageview mkmapview


【解决方案1】:

我假设您希望同时发生的两个动画是框架的旋转和移动?

要确保动画流畅并符合您的预期,最简单的方法是将指南针放在另一个视图中。

然后,您可以为罗盘父级的框架设置动画,以使罗盘移入和移出,并将旋转仅应用于罗盘本身。

显示

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

【讨论】:

  • 没有 [locationManager startUpdateHeading] 的内容?
  • 还是一样。即使我将启动命令放在您发送的显示语句中,它也会停止更新并且不会再次启动。 locationManager 只是不想再次开始更新或动画。
  • 不过,现在一切都运行得更顺畅了。 :)
  • 这可能是一个不同的问题,我们很可能需要查看您如何启动/停止位置管理器的代码
  • 我放了一个 nslog 看看会发生什么。 " NSLog(@"%f ​​(%f) => %f (%f)", manager.heading.trueHeading, oldRad, newHeading.trueHeading, newRad); ".一切正常,直到我解雇,当我再次显示它时,它不会再次开始更新,即使我添加了 [locationManager startUpdatingHeading];对它...有什么想法吗?
【解决方案2】:

正如 Paul.s 在 cmets 中提到的,这不是当前制作动画的首选方式。使用动画的块方法,您要问的问题真的很容易。看看documentation,这里有一个简短的例子。

-(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
                                          }
                          ];
                     }
    ];
}

【讨论】:

    猜你喜欢
    • 2023-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多