Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 2.36 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. having trouble creating ken burns effect with CALayer for iphone
  2. - (CALayer*)buildKenBurnsLayerWithImage:(UIImage *)image startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint fromScale:(float)fromScale toScale:(float)toScale
  3. {
  4.     float calFromScale = fromScale + 1;
  5.     float calToScale = toScale + 1;
  6.     float fromX = startPoint.x * calFromScale;
  7.     float fromY = (image.size.height * calFromScale) - (videoSize.height + (startPoint.y * calFromScale));
  8.     float toX = endPoint.x * calToScale;
  9.     float toY = (image.size.height * calToScale) - (videoSize.height + (endPoint.y * calToScale));
  10.  
  11.     CGPoint anchor = CGPointMake(0.0, 0.0);
  12.  
  13.     CALayer* imageLayer    = [CALayer layer];
  14.     imageLayer.contents    = (id)image.CGImage;
  15.     imageLayer.anchorPoint = anchor;
  16.     imageLayer.bounds      = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
  17.     imageLayer.position    = CGPointMake(image.size.width * anchor.x, image.size.height * anchor.y);
  18.     imageLayer.contentsGravity = kCAGravityResizeAspect;
  19.  
  20.     // create the panning animation.
  21.     CABasicAnimation* panningAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
  22.     panningAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(-fromX, -fromY)];
  23.     panningAnimation.toValue   = [NSValue valueWithCGPoint:CGPointMake(-toX, -toY)];
  24.     panningAnimation.additive = YES;
  25.     panningAnimation.removedOnCompletion = NO;
  26.  
  27.     // create the scale animation.
  28.     CABasicAnimation* scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  29.     scaleAnimation.fromValue = [NSNumber numberWithFloat:fromScale];
  30.     scaleAnimation.toValue = [NSNumber numberWithFloat:toScale];
  31.     scaleAnimation.additive = YES;
  32.     scaleAnimation.removedOnCompletion = NO;
  33.  
  34.     CAAnimationGroup* animationGroup = [CAAnimationGroup animation];
  35.     animationGroup.animations = [[NSMutableArray alloc] initWithObjects:panningAnimation,scaleAnimation, nil];
  36.     animationGroup.beginTime = 1e-100;
  37.     animationGroup.duration = 5.0;
  38.     animationGroup.removedOnCompletion = NO;
  39.     animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
  40.  
  41.     [imageLayer addAnimation:animationGroup forKey:nil];
  42.  
  43.     return imageLayer;
  44. }
  45.        
  46. CALayer* animatedLayer = [self buildKenBurnsLayerWithImage:image startPoint:CGPointMake(100, 100) endPoint:CGPointMake(500, 500) fromScale:5.0 toScale:2.0];