当呈现另一个UIViewController时,UIView动画暂停

时间:2017-12-01 20:05:42

标签: ios objective-c uiview uiviewcontroller

我有一个UITabBarController作为我的应用程序的rootViewController。启动下载后,我会在最后一个标签上显示徽章并使用脉冲颜色显示正在进行下载:

- (void)incrementBadgeValue {

    UITabBarItem *moreTabBarItem = [self.tabBar.items lastObject];
    moreTabBarItem.badgeValue = @"↓";

    self.originalColor = moreTabBarItem.badgeColor.copy;
    UIColor *toColor = [UIColor darkGrayColor];
    NSTimeInterval duration = 1.5f;

    [UIView animateWithDuration:duration
                          delay:0.0
                        options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut
                     animations:^{

                         moreTabBarItem.badgeColor = toColor;

                     } completion:^(BOOL finished) {
                         if (finished) {
                             moreTabBarItem.badgeColor = self.originalColor;
                         }
                     }];

}

这非常有效,直到窗口上显示另一个UIViewController,或者用户离开并返回到应用。在这些情况下,动画暂停/停止。

在这种情况下如何继续播放此动画?

1 个答案:

答案 0 :(得分:0)

我有同样的问题。要解决它

  • 创建UITabBarController(名称CustomTabBarController
  • 的子类
  • 覆盖viewWillAppear方法以在窗口上显示另一个UIViewController时停止动画,或者用户离开并返回应用
  • 覆盖viewDidDisappear方法以检查并在需要时继续动画。
  • 如果您无法将动画代码放在CustomTabBarController内。创建delegate并在控制器类上使用它

<强> CustomTabBarController

@protocol CustomTabBarDelegate<NSObject>

- (void)tabBarWillAppear;
- (void)tabBarDidDisappear;

@end

@interface CustomTabBarController : UITabBarController

@property(nonatomic, weak) id<CustomTabBarDelegate> customDelegate;

@end

@implementation CustomTabBarController

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];

  [self.customDelegate tabBarWillAppear];
}

- (void)viewDidDisappear:(BOOL)animated {
  [super viewDidDisappear:animated];

  [self.customDelegate tabBarDidDisappear];
}

@end

<强> YourViewController

@interface YourViewController () <CustomTabBarDelegate>

@end

@implementation YourViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  [(CustomTabBarController*)self.tabBarController setCustomDelegate:self];
}

- (void)tabBarWillAppear {
  if (/*Still need animation*/) {
    [self incrementBadgeValue];
  }
}

- (void)tabBarDidDisappear {
  // Remove your animation
}

@end