ios - 暂停计时器、UIView 动画和音频 iOS

标签 ios xcode avaudioplayer nstimer uiviewanimation

我有一个 viewController,我正在其中使用 AVAudioPlayer 播放声音并且计时器使用 NSTimer 运行 2 分钟并且正在使用播放一系列动画

 [UIView animateWithDuration:2.0
                          delay:0.0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         //Rolling the Man's tongue out
                         _cooling2ManImage.alpha = 1.0;
                         _cooling3ManImage.alpha = 0.0;
                     }
                     completion:^(BOOL boolValue){
                         [UIView animateWithDuration:2.0
                                               delay:0.0
                                             options:UIViewAnimationOptionCurveLinear
                                          animations:^{
                                              //showing the Arrow image and translating the arrow to the mouth
                                              _cooling1ArrowImage.alpha = 1.0;
                                              _cooling1ArrowImage.transform = CGAffineTransformMakeTranslation(0,-110);
                                          }
                                          completion:^(BOOL boolValue){
                                              [UIView animateWithDuration:1.5
                                                                    delay:0.0
                                                                  options:UIViewAnimationOptionCurveLinear
                                                               animations:^{
                                                                   //Hiding the arrow
                                                                   _cooling1ArrowImage.alpha = 0.0;
                                                               }
                                                               completion:^(BOOL boolValue){
                                                                   _cooling1ArrowImage.transform = CGAffineTransformMakeTranslation(0,0);
                                                                   [UIView animateWithDuration:2.0
                                                                                         delay:0.0
                                                                                       options:UIViewAnimationOptionCurveLinear
                                                                                    animations:^{
                                                                                        //Roll the tongue back in
                                                                                        _cooling2ManImage.alpha = 0.0;
                                                                                        _cooling3ManImage.alpha = 1.0;
                                                                                    }
                                                                                    completion:^(BOOL boolValue){
                                                                                        //Show the arrow and translate it
                                                                                        [UIView animateWithDuration:2.0
                                                                                                              delay:0.0
                                                                                                            options:UIViewAnimationOptionCurveLinear
                                                                                                         animations:^{
                                                                                                             _cooling3ArrowImage.alpha = 1.0;
                                                                                                             CGAffineTransform scale;
                                                                                                             CGAffineTransform translate;
                                                                                                             scale = CGAffineTransformMakeScale(2.5, 2.5);
                                                                                                             translate = CGAffineTransformMakeTranslation(0, 40);
                                                                                                             _cooling3ArrowImage.transform = CGAffineTransformConcat(scale, translate);
                                                                                                             _cooling3ArrowImage.alpha = 0.0;
                                                                                                         }
                                                                                                         completion:^(BOOL boolValue){
                                                                                                             [UIView animateWithDuration:2.0
                                                                                                                                   delay:0.0
                                                                                                                                 options:UIViewAnimationOptionCurveLinear
                                                                                                                              animations:^{
                                                                                                                                  _cooling2ManImage.alpha = 0.0;
                                                                                                                                  _cooling3ManImage.alpha = 1.0;
                                                                                                                                  _cooling1ArrowImage.alpha = 0.0;
                                                                                                                                  _cooling3ArrowImage.alpha = 0.0;
                                                                                                                                  _cooling3ArrowImage.transform = CGAffineTransformMakeTranslation(0,0);
                                                                                                                                  _cooling3ArrowImage.transform = CGAffineTransformMakeScale(1,1);
                                                                                                                                  if (count <= 6 ) {
                                                                                                                                      [self startAnimation1];
                                                                                                                                      count += 1;
                                                                                                                                      NSLog(@"%i",count);
                                                                                                                                  }
                                                                                                                              }
                                                                                                                              completion:nil];
                                                                                                         }];
                                                                                    }];
                                                               }];
                                          }];
                     }];

我必须实现暂停和恢复功能,以便当用户点击“暂停”按钮时,音频、动画和计时器暂停,当用户点击恢复按钮时,它会从暂停的位置准确恢复。谁能告诉我如何实现这一目标?

最佳答案

供 AVAudioPlayer 使用

[avplayerObject pause];
[avplayerObject play];

for timer:

您可以存储自计时器启动以来耗时量...当计时器启动时,将日期存储在 NSDate 变量中。然后,当用户切换时...使用 NSDate 类中的 timeIntervalSinceNow 方法来存储已经过去了多少时间...请注意,这将为 timeIntervalSinceNow 提供负值。当用户返回时,使用该值来设置适当的计时器。

用于查看动画 check this one

关于ios - 暂停计时器、UIView 动画和音频 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16210466/

相关文章:

ios - 访问 View super View 上的方法 - 什么是 UIViewControllerWrapperView?

ios - 如何避免在 iOS 中对 CATransform3D 旋转进行倾斜/翻转?

ios - CGAffineTransformMakeScale 的隐式声明无效

ios - 针对不同构建配置的框架的 Xcode 项目文件引用路径

swift - GCD + Firebase 云存储

iphone - 第一次播放声音时 AVAudioPlayer 启动缓慢

ios - CloudKit,我的授权不工作

ios - 通过 LongPress 更改 UITextField 背景颜色

ios - iPhone AVAudioPlayer 应用程序在第一次播放时卡住

iphone - AVPlayer 的音频电平表