一个处理程序重复的UIView animateWithDuration

一个处理程序重复的UIView animateWithDuration?


我的UIView animateWithDuration的重复我的观点动画。我怎样才可以有一个处理程序,可以在以后停止这个动画?例如,重复的动画开始的,我需要从以后停止
-------------------------------------------------------------------------------
1. 你可以做这样的假设你已经创建了一个取消的财产。作为块的startAnimation调用记录需要被包裹在一个异步调用,以避免堆栈溢出.a定要与任何类类型你确实有更换“身份证”。

- (void)startAnimation {
 [UIView animateWithDuration:1.0
       delay:0.0
      options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
      animations:^(void) {
       //animate
      }
      completion:^(BOOL finished) {
       if(!self.canceled) {
        __weak id weakSelf = self;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
         [weakSelf startAnimation];
        }];
       }
      }
  ];
}


2. 动画的目的是要反复进行动画图象的反弹。当没有担心手动停止它,那么你只需要设置三个属性(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat)和用于移动图像动画块代码-self.image.center = CGPointMake(self.image.center.x, self.image.center.y+25);下面是动画的完整的代码:

[UIView animateWithDuration:0.5 delay:0 options:( UIViewAnimationOptionCurveEaseIn | 
 UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | 
 UIViewAnimationOptionAllowUserInteraction) animations:^{self.image.center = 
 CGPointMake(self.image.center.x, self.image.center.y+25);} completion:nil];

就是这样。但是如果你需要一个手动控制,然后额外的代码是必需的。首先,根据jaminguy,你需要有一个布尔属性指示回路/停止(self.playAnimationForImage)的动画,并用干净的动画代码 CodeGo.net,会从其他地方调用。这里是

-(void)animateImageBounce{
 [UIView animateWithDuration:0.5 delay:0 options:( 
 UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAutoreverse | 
 UIViewAnimationOptionAllowUserInteraction) animations:^{self.image.center = 
 CGPointMake(self.image.center.x, self.image.center.y+25);} completion:^(BOOL finished){if 
 (finished && self.playAnimationForImage){
 self.image.center = CGPointMake(self.image.center.x, self.image.center.y-25); 
 [self animateImageBounce];
 }}]; 

这里是从动画呼叫开始

-(void)someMethod{
...
self.playAnimationForFingers = YES;
[self animateImageBounce];

} 我想说明的一点是,在手动控制,需要重新设置图像的center.y右后方之前,未来的递归调用是 
3. 事实上,在递归调用的解决方案并没有制定出动画开始古怪的行为:每隔2-3 animatation重复周期我得到动画rest。项目的优先部分反弹后(移动项下)的第二部分(动起来)的执行几乎瞬间。我的事情是必须做的递归调用。 因此,我认为。该解决方案将是启动动画与自动翻转并重复选择和,块,以检查是否有标志(self.playAnimationForFingers)表示,以停止动画。

-(void)animateFingersForLevelCompleteScreen{
 //fix: reset the place of bouncing fingers (starting place). 
 //Otherwise the fingers will slowly move to the bottom at every level. 
 //This resetting is not working when placed inside UIView 
 //animate complete event block
 self.image.center = CGPointMake(10 + self.image.frame.size.width/2, 
         95 + self.image.frame.size.height/2); 
 [UIView animateWithDuration:0.5 delay:0 
 options:(UIViewAnimationOptionCurveEaseIn | 
    UIViewAnimationOptionAutoreverse | 
    UIViewAnimationOptionRepeat |
    UIViewAnimationOptionAllowUserInteraction) 
 animations:^{
  self.image.center = CGPointMake(self.image.center.x, 
          self.image.center.y+25);
 } 
 completion:^(BOOL finished){
  /*finished not approapriate: finished will not be TRUE if animation 
  was interrupted. It is very likely to happen because animation repeats && */
  if (!self.playAnimationForFingers){
   [UIView setAnimationRepeatAutoreverses:NO]; 
  }
 }];
}


4. u能CABasicAnimation代替。

 CABasicAnimation *appDeleteShakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
appDeleteShakeAnimation.autoreverses = YES;
appDeleteShakeAnimation.repeatDuration = HUGE_VALF;
appDeleteShakeAnimation.duration = 0.2;
appDeleteShakeAnimation.fromValue = [NSNumber numberWithFloat:-degreeToRadian(5)];
appDeleteShakeAnimation.toValue=[NSNumber numberWithFloat:degreeToRadian(5)];
[self.layer addAnimation:appDeleteShakeAnimation forKey:@"appDeleteShakeAnimation"];

然后当u想停止它,你可以调用

[self.layer removeAnimationForKey:@"appDeleteShakeAnimation"];
posted @ 2016-01-11 15:27  LoyaltyProgram  阅读(546)  评论(0编辑  收藏  举报