我试图使用setBeginTime将图层的不透明度和位置的动画延迟3秒.我已经调用了图层boxLayer.然而,动画在前3秒(该图层不应显示)中运行良好,图层将显示在其最终位置和不透明度.它不应该.组动画无法解决问题.有人可以帮忙吗?见下面的代码:
// Create an animation that will change the opacity of a layer
CABasicAnimation *fader = [CABasicAnimation animationWithKeyPath:@"opacity"];
// It will last 1 second and will be delayed by 3 seconds
[fader setDuration:1.0];
[fader setBeginTime:CACurrentMediaTime()+3.0];
// The layer's opacity will start at 0.0 (completely transparent)
[fader setFromValue:[NSNumber numberWithFloat:startOpacity]];
// And the layer will end at 1.0 (completely opaque)
[fader setToValue:[NSNumber numberWithFloat:endOpacity]];
// Add it to the layer
[boxLayer addAnimation:fader forKey:@"BigFade"];
// Maintain opacity to 1.0 JUST TO MAKE SURE IT …Run Code Online (Sandbox Code Playgroud) 例如,我有这个CAKeyFrameAnimation:
CALayer* theLayer = myView.layer;
CAKeyframeAnimation* animation;
animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.duration = 1.6;
//animation.cumulative = YES;
animation.repeatCount = 1;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0 * M_PI],
[NSNumber numberWithFloat:(15.0/180.0) * M_PI],
[NSNumber numberWithFloat:(30.0/180.0) * M_PI], // animation stops here...
[NSNumber numberWithFloat:(45.0/180.0) * M_PI], // ignored!
[NSNumber numberWithFloat:(190.0/180.0) * M_PI], nil]; // ignored!
animation.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:0.2],
[NSNumber numberWithFloat:0.4], // ignored!
[NSNumber numberWithFloat:0.8], // ignored!
[NSNumber numberWithFloat:1.6], nil]; // ignored! …Run Code Online (Sandbox Code Playgroud) 我想让我的CALayer动画显示一段时间,然后淡出,这是我的代码
[_msgLayer removeAllAnimations];
[UIView animateWithDuration:10 delay:0 options:0 animations:^ {
_msgLayer.hidden = false;
NSLog(@"showing");
} completion:^(BOOL finished) {
NSLog(@"showing done, finished=%d", finished);
[UIView animateWithDuration:10 delay:40 options:0 animations:^ {
_msgLayer.hidden = true;
} completion:^(BOOL hideFinished) {
NSLog(@"hiding done, finished=%d", hideFinished);
}];
}];
Run Code Online (Sandbox Code Playgroud)
然而,动画根本无法正常工作,一切都几乎立即完成
2014-10-26 10:11:28.249 Foobar[91761:6827358] showing
2014-10-26 10:11:28.254 Foobar[91761:6827358] showing done, finished=1
2014-10-26 10:11:28.255 Foobar[91761:6827358] hiding done, finished=1
Run Code Online (Sandbox Code Playgroud)
我看到一些类似的问题,有些人说hidden不可动画,但文件说它是可动画的.我也试过opacity,然后同样的结果,动画仍然立即完成.这是为什么?我该如何解决?
该_msgLayer是我自己的类继承CALayer的,它有自己的绘画方法.这个动画是从网络事件中调用的,就像服务器向iPhone发送消息一样,然后我在屏幕上显示消息.