连续显示多个CALayers 60秒?

时间:2012-06-26 19:53:50

标签: iphone objective-c ios calayer

我有这种情况。

我正在使用AVFoundation来捕捉相机框架。 现在我要做的是,对于某些帧,我需要显示一个逐步旋转的图片。

我要做的是,我正在拍摄4个CALayers,包括一个对象的前后左右图像,并使用CALayer时间属性和组动画属性,我想在一定毫秒后逐个显示所有图像秒间隔时间使连续图像看起来像动画。

如何去做?请在这里帮我编写一些代码。

2 个答案:

答案 0 :(得分:1)

-(void)startMainAnimation
{
  //Animationframes is array of images that should be CGImage Type not UIImage..
  //animation Layer that is added above view……


  CAKeyframeAnimation *frameAnimation = [[CAKeyframeAnimation alloc] init];
  [frameAnimation setKeyPath:@"contents"];
  frameAnimation.calculationMode = kCAAnimationDiscrete;
  [animationLayer setContents:[animationFrames lastObject]];
  frameAnimation.autoreverses = NO;
  frameAnimation.duration = ((float)[animationFrames count])/4.5;;
  frameAnimation.repeatCount = 1;
  [frameAnimation setValues:animationFrames];
  [frameAnimation setRemovedOnCompletion:YES];
  [frameAnimation setDelegate:self];
  [animationLayer addAnimation:frameAnimation forKey:@"contents"];
  [frameAnimation release];

}

答案 1 :(得分:1)

根据Mohit Gupta的贴身链接回答:

设置您想要图像序列动画的CALayer

CALayer *animationLayer = [CALayer layer];
[animationLayer setFrame:CGRectMake(125, 0, 240, 300)];
[self.baseLayer addSublayer:animationLayer];

定义需要在序列动画中显示的图像数组

NSArray *animationFrames = [NSArray arrayWithObjects:(id)[UIImageimageNamed:@"1.png"].CGImage, (id)[UIImage imageNamed:@"2.png"].CGImage, (id)[UIImage imageNamed:@"3.png"].CGImage, nil];

使用CAKeyframeAnimation以顺序方式显示图像数组

CAKeyframeAnimation *frameAnimation = [[CAKeyframeAnimation alloc] init];
[frameAnimation setKeyPath:@"contents"];
frameAnimation.calculationMode = kCAAnimationDiscrete; //mode of transformation of images
[animationLayer setContents:[animationFrames lastObject]]; //set the array objects as encounterd
frameAnimation.autoreverses = NO; //If set Yes, transition would be in fade in fade out manner
frameAnimation.duration = ((float)[animationFrames count])/4.5; //set image duration , it can be predefined float value
frameAnimation.repeatCount = HUGE_VAL; //this is for inifinite, can be set to any integer value as well
[frameAnimation setValues:animationFrames];
[frameAnimation setRemovedOnCompletion:YES];
[frameAnimation setDelegate:self];
[animationLayer addAnimation:frameAnimation forKey:@"contents"]; //add animation to your CALayer
[frameAnimation release];

希望这有帮助