Search code examples
iphoneobjective-cuiimagecaanimationcakeyframeanimation

Call custom function after arriving first caanimation point


I want to slide in a uiimage from right (out of screen) to the middle and then from the middle to the left (out of screen). When the image arrived (first point of the animation path) in the middle I want to call a custom function. How can I realize that?

thanks for help.

Thats what I tried, but its lagging a little bit in the middle (beginning of the second animation). I think its better to have one animation

my code:

- (void) animateFromRightToMiddlePath {

 CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
 pathAnimation.calculationMode = kCAAnimationPaced;
 pathAnimation.fillMode = kCAFillModeForwards;
 pathAnimation.removedOnCompletion = NO;
 pathAnimation.duration = 3;
 pathAnimation.repeatCount = 1;
 pathAnimation.delegate = self;
 [pathAnimation setValue:@"rightToMiddle" forKey:@"AnimationType"];


 CGMutablePathRef curvedPath = CGPathCreateMutable();
 CGPathMoveToPoint(curvedPath, NULL, x + (picture.size.width/2), 250);
 CGPathAddLineToPoint(curvedPath, NULL, (picture.size.width/2), 250);
 pathAnimation.path = curvedPath;
 CGPathRelease(curvedPath);
 imageView = [[UIImageView alloc] initWithImage:picture];
 imageView.tag = 1;
 imageView.frame = CGRectMake(1, 1, picture.size.width, picture.size.height);
 [self addSubview:imageView];
 [imageView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];

}

- (void) animateFromMiddleToLeftPath {

     CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
     pathAnimation.calculationMode = kCAAnimationPaced;
     pathAnimation.fillMode = kCAFillModeForwards;
     pathAnimation.removedOnCompletion = NO;
     pathAnimation.duration = 3; 
     pathAnimation.repeatCount = 1;
     pathAnimation.delegate = self;
     [pathAnimation setValue:@"middleToLeft" forKey:@"AnimationType"];

     CGMutablePathRef curvedPath = CGPathCreateMutable();
     CGPathMoveToPoint(curvedPath, NULL, (picture.size.width/2), 250);
     CGPathAddLineToPoint(curvedPath, NULL, -(picture.size.width/2), 250);
     pathAnimation.path = curvedPath;
     CGPathRelease(curvedPath);

     [imageView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];

}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{

    NSString *aniType = [theAnimation valueForKey:@"AnimationType"];

    if ([aniType isEqualToString:@"rightToMiddle"]) {

        [self animateFromMiddleToLeftPath];
     // CUSTOM FUNCTION CALL
    }

    if ([aniType isEqualToString:@"middleToLeft"]) {

//        [self animateFromRightToMiddlePath];

    }

}

Solution

  • A way to model this kind of interaction is the following:

    1. your image moves through the screen according to its own logic (a fixed animation, you drag it, etc);

    2. you define a timer so that at each tick a handler function is called; this handler has the responsibility (in your specific case) of checking the position of the image;

    3. when the position is what you want, the handler fires your other function.

    This could seem complex, but it is not really. It allows you to solve this problem in a modular and reusable way, and it is inspired by how games deal with this kind of problem.