我遇到了类似的问题。我使用图层而不是动画视图。你可以尝试这样的事情。
- 将每个元素绘制为 CALayer 并将它们作为容器 UIVIew 图层的子图层包含在内。 UIView 更容易制作动画,但您的控制权会更少。请注意,对于任何视图,您都可以使用 [view layer] 获取它的图层;
- 为您的四边形创建一个自定义子图层。该图层应该具有您想要为此图层设置动画的一个或多个属性。我们将此属性称为“customprop”。因为它是自定义图层,所以您需要在动画的每一帧上重绘。对于您计划设置动画的属性,您的自定义图层类应返回 YES needDisplayForKey:。这样你就可以确保-(void)drawInContext:(CGContextRef)theContext每帧都会被调用。
- 将所有动画(圆形和四边形)放在同一个事务中;
对于圆圈,您可以使用 CALayers 并设置内容,如果它是图像,则标准方式:
layer.contents = [UIImage imageNamed:@"circle_image.png"].CGImage;
现在,对于四层,子类 CALayer 并以这种方式实现:
- (void)drawInContext:(CGContextRef)theContext{
//Custom draw code here
}
+ (BOOL)needsDisplayForKey:(NSString *)key{
if ([key isEqualToString:@"customprop"])
return YES;
return [super needsDisplayForKey:key];
}
交易看起来像:
[CATransaction begin];
CABasicAnimation *theAnimation=[CABasicAnimation animationWithKeyPath:@"customprop"];
theAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(1000, 1000)];
theAnimation.duration=1.0;
theAnimation.repeatCount=4;
theAnimation.autoreverses=YES;
theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
theAnimation.delegate = self;
[lay addAnimation:theAnimation forKey:@"selecting"];
[CATransaction setValue:[NSNumber numberWithFloat:10.0f]
forKey:kCATransactionAnimationDuration];
circ1.position=CGPointMake(1000, 1000);
circ2.position=CGPointMake(1000, 1000);
[CATransaction commit];
现在所有抽奖例程将同时发生。确保您的drawInContext:实现速度很快。否则动画会滞后。
将每个子图层添加到 UIViews 图层后,请记住调用 [layer setNeedsDisplay]。它不会被自动调用。
我知道这有点复杂。但是,生成的动画比使用 NSTimer 并在每次调用时重绘要好。