【问题标题】:UIView animation flickering with autoreverseUIView动画随着自动反转闪烁
【发布时间】:2011-11-19 17:44:58
【问题描述】:

我正在尝试为一个空的表单域设置一个指示器的动画,所以我使用下面的方法来设置一个位置的动画,反转动画,然后重复。在模拟器中,这工作正常,在我的 3GS 上,当调用完成块时,它看起来好像有一个闪烁。指示器会短暂显示在中间位置,而不是回到它的原点。

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

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

【问题讨论】:

  • 仍未解决,但我找到了解决方法。我使用 UIViewAnimationOptionRepeat 选项并完全删除完成块。

标签: animation uikit


【解决方案1】:

我遇到了同样的问题,并前往 Apple DTS 寻求解决方法。

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

尤其是这种方式,因为文档指出,对于

UIViewAnimationOptionAutoreverse 向后运行动画并 前锋。

必须与 UIViewAnimationOptionRepeat 选项结合使用。

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

我的实现是动态的,因此您可能不必实现第一步,但我将其保留在这里仅供参考。

首先,我检查了 UIViewAnimationOptionAutoreverse 是否是我要传递到动画中的选项的一部分,而 UIViewAnimationOptionRepeat 不是...如果是,我将其从通过添加如下行来选择选项:

animationOptions &= ~UIViewAnimationOptionAutoreverse;

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

我的项目中的代码如下:

从对象的动画选项中去除自动反转选项的语句:

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

倒车不重复时调用的补全方法:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 2012-03-12
    • 2011-03-30
    • 2011-08-23
    • 1970-01-01
    相关资源
    最近更新 更多