我有一个UIView,我正在应用CAAnimation。 animation 是继承自CAKeyframeAnimation的类的实例,该键路径为@“transform.scale”。
[[myView layer] addAnimation:animation forKey:@"animation key"];
[[myView layer] setValue:[NSNumber numberWithBool:finalScale] forKey:@"transform.scale"];
像往常一样,除了添加动画之外,我正在设置值,以便当它结束时,它不会回到原始状态。
这一切都很好用。但是,如果我退出此控制器然后返回,不知何故该图层将此变换应用了两次,因此它看起来要小4倍,即使当前值为 finalScale 。
我尝试在viewWillDisappear上保存当前变换并再次在viewWillAppear上应用它。也在viewWillAppear上尝试了这个:
[myView.layer setTransform:CATransform3DIdentity];
但这些都没有奏效。看起来有一些其他的矩阵计算是在幕后制作的,还是我错过了什么?
编辑: 这就是我创建动画的方式:
double startValue = [[view.layer.presentationLayer valueForKeyPath:@"transform.scale"] doubleValue];
ExponentialAnimation *animation = [ExponentialAnimation animationWithKeyPath:@"transform.scale"
startValue:startValue
endValue:endValue
duration:kDuration];
[view.layer removeAnimationForKey:@"scale"];
[view.layer addAnimation:animation forKey:@"scale"];
[view.layer setValue:[NSNumber numberWithFloat:endValue] forKeyPath:@"transform.scale"];
ExponentialAnimation看起来像这样:
@interface ExponentialAnimation : CAKeyframeAnimation
{
double _startValue;
double _endValue;
NSUInteger _steps;
}
@implementation ExponentialAnimation
@synthesize startValue=_startValue,
endValue=_endValue;
+ (id)animationWithKeyPath:(NSString *)keyPath
startValue:(double)startValue
endValue:(double)endValue
duration:(CGFloat)duration
{
return [[[self alloc] initWithKeyPath:keyPath
startValue:startValue
endValue:endValue
duration:duration] autorelease];
}
- (id)initWithKeyPath:(NSString *)keyPath
startValue:(double)start
endValue:(double)end
duration:(CGFloat)duration
{
if ((self = [super init]))
{
self.keyPath = keyPath;
self.duration = duration;
_startValue = start;
_endValue = end;
_steps = [self numberOfStepsForDuration:duration withFPS:kAnimationFPS];
[self calculateKeyFrames];
}
return self;
}
calculateSteps只为每个关键帧创建一个值数组,并将其添加为:
[self setValues:values];
当viewController消失时, view 将比例设置为0.4(动画中的 endValue )。当我回去时,获得[view.layer valueForKeyPath:@“transform.scale”]的值为0.4,但你真正看到的是0.4 * 0.4。