ios - block 动画立即完成,但 CABasicAnimation 可以与自定义动画属性一起正常工作

标签 ios objective-c animation objective-c-blocks caanimation

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

我创建了一个带有自定义图层的 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)needsDisplayForKey:(NSString *)key {
    if( [key isEqualToString:@"inputValue"] )
        return YES;
    return [super needsDisplayForKey:key];
}

- (void)setInputValue:(CGFloat)inputValue {
    _inputValue = inputValue;
    [self setNeedsDisplay];
}

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

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

最佳答案

不要混合 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/

相关文章:

ios - 在 iOS 中执行 'order by date' 查询所需的日期格式

ios - 将 Swift 函数转换为 Objective C 并在 swift 中使用返回值

ios - 四舍五入到特定值?

ios - 结合许多 UIViewPropertyAnimators

iphone - 如何在 iPhone 中使用 HTML 文件

objective-c - iOS:处理重叠的后台请求

ios - 在 iOS 中播放准备好的动画

iphone - 如何在 iPhone 中从左到右设置动画 View ?

iOS 设备的 HTML/CSS 悬停翻转可见性问题

ios - 如何围绕圆形 UIView 制作弯曲的进度条?