IOS-编码!
此代码用于动画小红方以绘制大符号" 8"在UIView
上。首先是上环(animSequence1
= 5秒),然后是下环(animSequence2
=另外5秒)。
不需要延迟!
但我坚持两个连续动画之间的奇怪延迟(约1秒)。这种延迟是从哪里来的?!?
代码:
- (void)drawRect:(CGRect)drawRect {
CGContextRef context = UIGraphicsGetCurrentContext();
drawRect = CGRectMake(0, 0, 320, 320);
CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor;);
CGContextFillRect(context, drawRect);
// Little red square
CALayer *layerTest;
layerTest = [CALayer layer];
layerTest.bounds = CGRectMake(0, 0, 20, 20);
layerTest.anchorPoint = CGPointMake(0, 0);
layerTest.backgroundColor = [UIColor redColor].CGColor;
[[self layer] addSublayer:layerTest];
// Upper ring
CAKeyframeAnimation *animSequence1;
animSequence1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animSequence1.fillMode = kCAFillModeForwards;
animSequence1.removedOnCompletion = NO;
animSequence1.autoreverses = NO;
animSequence1.repeatCount = 0;
animSequence1.duration = 5.0;
animSequence1.beginTime = 0.0;
animSequence1.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 120) radius:80 startAngle:DEG_TO_RAD(90) endAngle:DEG_TO_RAD(450) clockwise:YES].CGPath;
animSequence1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
// Lower ring
CAKeyframeAnimation *animSequence2;
animSequence2 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animSequence2.fillMode = kCAFillModeForwards;
animSequence2.removedOnCompletion = NO;
animSequence2.autoreverses = NO;
animSequence2.repeatCount = 0;
animSequence2.duration = 5.0;
animSequence2.beginTime = 5.0;
animSequence2.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 280) radius:80 startAngle:DEG_TO_RAD(-90) endAngle:DEG_TO_RAD(-450) clockwise:NO].CGPath;
animSequence2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
// A sequence of animations
CAAnimationGroup *animGroup;
layerTest.position = CGPointMake(200, 200);
animGroup = [CAAnimationGroup animation];
animGroup.duration = 10.0;
animGroup.animations = [NSArray arrayWithObjects:animSequence1, animSequence2, nil];
[layerTest addAnimation:animGroup forKey:nil];
}
我也试过没有CAAnimationGroup
为第一个[animSequence1 setDelegate:self];
(CAKeyframeAnimation
)设置animSequence1
,然后按CAKeyframeAnimation
开始第二个animSequence2
((void)animationDidStop
)。
它工作原理相同,但两个动画之间的这种奇怪的延迟(大约1秒)没有消失!
问题:如何消除两个连续CAKeyframeAnimation
动画之间的奇怪延迟? 不需要延迟!
答案 0 :(得分:2)
不要在-drawRect:
中创建图层或动画 - 您无法控制何时调用它们,因此您最终会添加多个layerTest
s在你看来,所有人都试图单独制作动画。在初始化程序中创建图层/动画,或者响应某些用户交互。
我还建议您删除fillMode
上的removedOnCompletion
和animSequence1
设置;一旦animSequence2
启动,您就会有效地要求CA在同一属性上运行两次非加法动画。不确定这种行为是否定义明确,但它是另一个可能来自古怪的地方。