导航后,视图停止动画

时间:2014-09-15 09:54:36

标签: ios objective-c animation uiview cabasicanimation

我创建自定义加载指示器。所以,我像这样更新我的观点

@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;
}

动画从这里开始

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

我有一个标签栏应用,所以问题是当动画开始时,我点击另一个标签然后返回到第一个视图,我的自定义指标没有动画。如果我将指示符添加到单元格,此错误(功能)也会出现在集合视图单元格中。 我通过调用简单方法addSubview来添加我的指标:

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

如何在导航后阻止停止动画? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

你走了,

//当用户点击tabBar中的不同视图时,我使用了BOOL viewChange来检查微调器是否正在运行。

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

希望这会有所帮助,如果有任何疑问,请告诉我。此外,如果可能的话,你应该避免用户在spinnerView运行时移动到另一个视图,因为它表明某种服务或更新正在运行,此时用户应该移动到另一个视图,直到它完成。