2011-09-18 164 views
6

我试图动画指示器到一个空的表单字段,所以我使用下面的方法动画到一个位置,扭转动画,并重复。在模拟器中,这可以正常工作,在我的3GS上,当完成块被调用时,它看起来好像闪烁了。指标简要显示在中间位置,而不是回到原点。UIView动画闪烁与自动反向

有关为什么会发生这种情况的任何想法?谢谢。

- (void)bounceFormIndicator { 
    if (formIndicator.superview == nil) { 
     return; 
    } 

    int bounceDistance = 24; 

    [UIView animateWithDuration:0.6 
          delay:0 
         options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction 
        animations:^{ 
         CGRect indicatorFrame = formIndicator.frame; 
         indicatorFrame.origin.x += bounceDistance; 
         formIndicator.frame = indicatorFrame; 
        }completion:^(BOOL finished){ 
         CGRect indicatorFrame = formIndicator.frame; 
         indicatorFrame.origin.x -= bounceDistance; 
         formIndicator.frame = indicatorFrame; 
         [self bounceFormIndicator]; 
        }]; 
} 
+0

还是没有解决,但我找到了解决办法。我使用UIViewAnimationOptionRepeat选项并完全删除完成块。 – brianpartridge

回答

13

我有同样的问题,并去苹果DTS帮助解决方法。

根据DTS,这种“闪烁”效果或反弹效果是预期的行为......我认为我在很长时间内对我的项目做了错误的事情。

尤其是这样,因为文档状态,为

UIViewAnimationOptionAutoreverse运行动画向后和向前 。

必须与UIViewAnimationOptionRepeat选项结合使用。

为了让闪烁消失,我必须做2件事。

我的实现是动态的,所以你可能不需要实现第一步,但我会保留在这里仅供参考。

首先,我检查,看看是否UIViewAnimationOptionAutoreverse是的,我要通过进入我的动画选项的一部分,并UIViewAnimationOptionRepeat不是 ......如果是这样,我剥夺它通过增加线路,如选择:

animationOptions &= ~UIViewAnimationOptionAutoreverse; 

要创建反转动画而不重复,我添加了一个相反的UIView动画作为我的完成块。我倒也放松,如果这是不是UIViewAnimationOptionCurveEaseInUIViewAnimationOptionCurveEaseOut ...

从我的项目中的代码如下:

这条从对象的animationOptions的自动翻转选项声明:

if ((animationOptions & AUTOREVERSE) == AUTOREVERSE) { 
    self.shouldAutoreverse = YES; 
    animationOptions &= ~AUTOREVERSE; 
} 

的处理动画的重写属性设置器的示例:

-(void)setCenter:(CGPoint)center { 
    CGPoint oldCenter = CGPointMake(self.center.x, self.center.y); 

    void (^animationBlock) (void) =^{ super.center = center; }; 
    void (^completionBlock) (BOOL) = nil; 

    BOOL animationShouldNotRepeat = (self.animationOptions & REPEAT) != REPEAT; 
    if(self.shouldAutoreverse && animationShouldNotRepeat) { 
     completionBlock =^(BOOL animationIsComplete) { 
      [self autoreverseAnimation:^ { super.center = oldCenter;}]; 
     }; 
    } 
    [self animateWithBlock:animationBlock completion:completionBlock]; 
} 

在t中调用的完成方法他倒车的情况下无需重复:

-(void)autoreverseAnimation:(void (^)(void))animationBlock { 
     C4AnimationOptions autoreverseOptions = BEGINCURRENT; 
     if((self.animationOptions & LINEAR) == LINEAR) autoreverseOptions |= LINEAR; 
     else if((self.animationOptions & EASEIN) == EASEIN) autoreverseOptions |= EASEOUT; 
     else if((self.animationOptions & EASEOUT) == EASEOUT) autoreverseOptions |= EASEIN; 

     [UIView animateWithDuration:self.animationDuration 
           delay:0 
          options:autoreverseOptions 
         animations:animationBlock 
         completion:nil]; 
}