OGeek|极客世界-中国程序员成长平台

标题: ios - block 动画立即完成,但 CABasicAnimation 与自定义动画属性正常工作 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:07
标题: ios - block 动画立即完成,但 CABasicAnimation 与自定义动画属性正常工作

有很多相关的问题,但这种情况似乎没有任何现有问题解决。

我创建了一个带有自定义层的 View ,以便可以为其中一个属性设置动画。使用 CABasicAnimation 类,动画可以正常工作。

但是,我需要对动画进行更多控制,例如缓入和缓出以及顺序动画,并尝试切换到使用 block 动画。但是,当我这样做时,动画会立即完成,而不是随着时间的推移制作动画。

我怎样才能让这个 block 动画正常工作?

工作动画代码:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath"inputValue"];
animation.duration = DEFAULT_ANIMATION_DURATION;

if (flipped) {
    animation.fromValue = [NSNumber numberWithDouble:0.0];
    animation.toValue = [NSNumber numberWithDouble:1.0];
    self.myLayer.inputValue = 1.0;
} else {
    animation.fromValue = [NSNumber numberWithDouble:1.0];
    animation.toValue = [NSNumber numberWithDouble:0.0];
    self.myLayer.inputValue = 0.0;
}

[self.layer addAnimation:animation forKey"animateInputValue"];

错误地立即完成的动画,但 finished 为 YES:

[UIView animateWithDuration:10.0 delay:0.0 options:0 animations:^{
    self.myLayer.inputValue = 1.0;
} completion:^(BOOL finished) {
    NSLog(@"done %@", finished?@"and finished"", but not finished");
}];

正在动画的 CALayer:

#import "UViewLayer.h"
#import "YoYouStyleKit.h"

@implementation UViewLayer

+ (BOOL)needsDisplayForKeyNSString *)key {
    if( [key isEqualToString"inputValue"] )
        return YES;
    return [super needsDisplayForKey:key];
}

- (void)setInputValueCGFloat)inputValue {
    _inputValue = inputValue;
    [self setNeedsDisplay];
}

- (void)drawInContextCGContextRef)context {
    UIGraphicsPushContext(context);
    [YoYouStyleKit drawUShapeWithFrame:self.bounds input:self.inputValue];
    UIGraphicsPopContext();
}

在自定义层中添加@dynamic inputValue;似乎没什么区别。



Best Answer-推荐答案


不要混合使用 UIKit 和 Core Animation 动画。

这样实现:

[CATransaction begin];

[CATransaction setCompletionBlock:^
{
    NSLog(@"done");
}];

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath"inputValue"];
animation.duration = DEFAULT_ANIMATION_DURATION;

if (flipped)
{
    animation.fromValue = [NSNumber numberWithDouble:0.0];
    animation.toValue = [NSNumber numberWithDouble:1.0];
    self.myLayer.inputValue = 1.0;
} 
else 
{
    animation.fromValue = [NSNumber numberWithDouble:1.0];
    animation.toValue = [NSNumber numberWithDouble:0.0];
    self.myLayer.inputValue = 0.0;
}

[self.layer addAnimation:animation forKey"animateInputValue"];

[CATransaction commit];

关于ios - block 动画立即完成,但 CABasicAnimation 与自定义动画属性正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26414531/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4