使用多个UIViewPropertyAnimators后Alpha无法正常工作

时间:2018-10-11 10:21:54

标签: objective-c animation uiviewpropertyanimator

我必须创建一个交互式动画(动画的入职序列),该动画由滚动视图的滚动偏移量引导。

为此,我尝试使用UIViewPropertyAnimator,这使我很容易设置fractionComplete属性。

当我使用0到4的时间刻度并以0..1、1..2等间隔使用4个不同的动画师时,问题就来了。移动,缩放旋转,调整颜色以及几乎所有功能都可以按照我的预期进行,但是设置Alpha似乎只能记住最后的动画属性。例如,如果我在每个动画中在0和1之间切换Alpha,则它只能向前播放,并且在播放第四个动画后向后滚动不会播放前三个动画的Alpha更改,而不会播放第三个动画的起始值。

示例代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.animator1 = [[UIViewPropertyAnimator alloc] initWithDuration:1.f curve:UIViewAnimationCurveLinear animations:^{
        self.leftConstraint.constant = 200.f;
        self.redBox.alpha = 0.1f;
        [self.view layoutIfNeeded];
    }];

    self.animator2 = [[UIViewPropertyAnimator alloc] initWithDuration:1.f curve:UIViewAnimationCurveLinear animations:^{
        self.topConstraint.constant = 200.f;
        self.redBox.alpha = 1.f;
        [self.view layoutIfNeeded];
    }];

    self.animator3 = [[UIViewPropertyAnimator alloc] initWithDuration:1.f curve:UIViewAnimationCurveLinear animations:^{
        self.leftConstraint.constant = 30.f;
        self.redBox.alpha = 0.4f;
        [self.view layoutIfNeeded];
    }];

    self.animator4 = [[UIViewPropertyAnimator alloc] initWithDuration:1.f curve:UIViewAnimationCurveLinear animations:^{
        self.topConstraint.constant = 30.f;
        self.redBox.alpha = 1.f;
        [self.view layoutIfNeeded];
    }];
}

- (IBAction)sliderChanged:(id)sender {
    CGFloat value = self.slider.value;

    self.label.text = [NSString stringWithFormat:@"%.2f", value];

    if (value < 0) {
        // pass
    } else if (value < 1.f) {
        self.animator1.fractionComplete = value;
        self.currentAnimatorLabel.text = [NSString stringWithFormat:@"a1 %.2f", value];
    } else if (value < 2.f) {
        self.animator2.fractionComplete = value - 1.f;
        self.currentAnimatorLabel.text = [NSString stringWithFormat:@"a2 %.2f", value - 1.f];
    } else if (value < 3.f) {
        self.animator3.fractionComplete = value - 2.f;
        self.currentAnimatorLabel.text = [NSString stringWithFormat:@"a3 %.2f", value - 2.f];
    } else if (value < 4.f) {
        self.animator4.fractionComplete = value - 3.f;
        self.currentAnimatorLabel.text = [NSString stringWithFormat:@"a4 %.2f", value - 3.f];
    }

    [self refreshDebugLabel];
}

示例项目: https://github.com/gklka/AnimatorTest/tree/master/AnimatorTest

插图: Animation

我想念什么吗?

0 个答案:

没有答案