Kevin Glier Kevin Glier - 4 months ago 36
Objective-C Question

Problems with creating a Ken-Burns Effect

I'm currently trying to create a Ken Burns effect on an UIImageView. It firstly should zoom-in (slowly) and after that, the didStopSelector of the Animation should call a method, which should zoom-out. The Problem is, that the first animation (zoom-in) is okay and works perfectly, as long as I don't add the didStopSelector to the animation. If I do so, it seems like the method is called directly (not after it didStop).

Here are the 2 Methods which include the animations:

- (void)beginKenBurnsEffect {

[UIView beginAnimations:@"a" context:self.view_image];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:5];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDidStopSelector:@selector(endKenBurnsEffect)];
[UIView setAnimationDelegate:self];


self.view_image.transform = CGAffineTransformScale(self.view_image.transform, 1.06, 1.06);
self.view_image.center = CGPointMake(self.frame.size.width/1.7, self.frame.size.height/2);

[UIView commitAnimations];
}

- (void)endKenBurnsEffect {

[UIView beginAnimations:@"b" context:self.view_image];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:5];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDidStopSelector:@selector(beginKenBurnsEffect)];
[UIView setAnimationDelegate:self];

self.view_image.transform = self.origTransform;
self.view_image.center = self.origPoint;

[UIView commitAnimations];
}


After I initialized the UIImageView, I save the current Transform and Center values to a property.

self.origTransform = self.view_image.transform;
self.origPoint = self.view_image.center;


I also tried it with only one animation and setAnimationAutoReverse, but after the animation is done, it zooms-in without animation (after it did zoom-out slowly animated).

Maybe you have an idea what the problem could be.

Thank you in advance :)

Answer

Have you tried using a CAAnimationGroup and using a few CAAnimations?

NSMutableArray * animations = [ NSMutableArray array ] ;
{
    CAAnimation * anim = [ CABasicAnimation animationWithKeyPath:@"transform" ] ;
    anim.fromValue = [ NSValue valueWithCGAffineTransform:CGAffineTransformScale(...) ] ;
    [ array addObject:anim ] ;
}
{
    CAAnimation * anim = // make intro position transform
    [ array addObject:anim ] ;
}

{
    CAAnimation * anim = // make outro position transform for layer.transform
    anim.offset = 5.0;
}

{
    CAAnimation * anim = // make outro position transform for layer.position
    anim.offset = 5.0;
}

CAAnimationGroup * group = [ CAAnimationGroup animation ] ;
group.animations = animations ;

[ CATransaction begin ] ;
[ self.view.layer addAnimation:group forKey:nil ] ;
[ CATransaction commit ] ;

... this is just a rough draft

Comments