简体   繁体   English

导航后视图停止动画

[英]View stops animation after navigation

I create my custom loading indicator. 我创建了自定义加载指示器。 So, I update my view like this 所以,我这样更新自己的观点

@property (nonatomic, readonly) CAShapeLayer *progressLayer;
@property (nonatomic, readwrite) BOOL isAnimating;

- (void)layoutSubviews {
    [super layoutSubviews];

    self.progressLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
    [self updatePath];
}

- (void)updatePath {
    CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    CGFloat radius = MIN(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2) - self.progressLayer.lineWidth / 2;
    CGFloat startAngle = 3 * M_PI_2;
    CGFloat endAngle = self.endAngle;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius   startAngle:startAngle endAngle:endAngle clockwise:YES];
    self.progressLayer.path = path.CGPath;
}

Animation starts here 动画从这里开始

- (void)startAnimating {
    if (self.isAnimating)
    return;

    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform.rotation";
    animation.duration = 1.0f;
    animation.fromValue = @(0.0f);
    animation.toValue = @(2 * M_PI);
    animation.repeatCount = INFINITY;

    [self.progressLayer addAnimation:animation forKey:kLLARingSpinnerAnimationKey];
    self.isAnimating = true;
}

- (void)stopAnimating {
    if (!self.isAnimating)
        return;

    [self.progressLayer removeAnimationForKey:kLLARingSpinnerAnimationKey];
    self.isAnimating = false;
}

- (CAShapeLayer *)progressLayer {
    if (!_progressLayer) {
        _progressLayer = [CAShapeLayer layer];
        _progressLayer.strokeColor = self.tintColor.CGColor;
        _progressLayer.fillColor = nil;
        _progressLayer.lineWidth = 1.5f;
    }
    return _progressLayer;
}

I have a tab bar app, so the problem is when animation start, I click to another tab and then return to first view, my custom indicator doesn't animate. 我有一个选项卡栏应用程序,所以问题是动画开始时,我单击到另一个选项卡,然后返回到第一视图,我的自定义指示器没有动画。 Also this bug(feature) appear in collection view cells if I add my indicator to cells. 如果我将指标添加到单元格,此错误(功能)也会出现在集合视图单元格中。 I add my indicator by calling simple method addSubview: 我通过调用简单的方法addSubview添加指标:

[view addSubview:indicator];
[indicator startAnimating];

How can I prevent stop animating after navigation? 如何防止导航后停止动画设置? Thanks in advance. 提前致谢。

Here you go, 干得好,

//I have used a BOOL viewChange to check if spinner was running or not while user tapped to different view in tabBar. //我使用了BOOL viewChange来检查在用户轻触tabBar中的其他视图时微调器是否正在运行。

- (void)viewWillAppear:(BOOL)animated{  //On initial run the viewChange will be false and the condition in if won't run but as spinnerView start animating u could change the viewChange flag to true.

  //So when user will be back from second view then this flag would be true and it will start animating.

  //As your work is complete then just stop animating and remove from view.

  if(viewChange){
     [spinnerView stopAnimating];
     [spinnerView removeFromSuperview];
     spinnerView = nil;
     spinnerView = [[LLARingSpinnerView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2, 40, 40)];

     [self.view addSubview:spinnerView];
     [spinnerView startAnimating];
  }
}

- (void)viewWillDisappear:(BOOL)animated{

//This is called when user move from current view to another and at that point we user won't be seeing the spinnerView so remove it and release it only if it's animating.

  if(spinnerView.isAnimating){
    viewChange = YES;
    [spinnerView stopAnimating];
    [spinnerView removeFromSuperview];
    spinnerView = nil;
  }
  else{
    viewChange = NO;
 }
}

- (IBAction)stopBtn:(id)sender{        //Spinner is stop animating with tap on button and is removed from the view.
  if(spinnerView.isAnimating){
      [spinnerView stopAnimating];
      [spinnerView removeFromSuperview];
      spinnerView = nil;               //release the obj of spinnerView.
  }
}

- (IBAction)btnTap:(id)sender{         //Spinner is start animating with tap on button
  if(!spinnerView)
    spinnerView = [[LLARingSpinnerView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2, 40, 40)];
  spinnerView.lineWidth = 1.5f;
  spinnerView.tintColor = [UIColor redColor];
  [self.view addSubview:spinnerView];

  [spinnerView startAnimating];
}

Hope this helps out and if any doubt do let me know. 希望这会有所帮助,如有疑问,请告诉我。 Also if possible u should avoid user to move to another view when spinnerView is running as it indicates that some sort of service or update is running and at that point user should move to another view till it's finish. 另外,如果可能,您应避免在spinnerView运行时避免用户移至另一个视图,因为它表明某种服务或更新正在运行,此时用户应移至另一个视图,直到完成为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM