有很多相关问题,但这种情况似乎没有任何现有问题可以解决。
我创建了一个带有自定义图层的 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/