I have an animation involving an object continuously bouncing back and forth between two walls with a time period of 2 seconds between two positions given by the CGPoints 'positionStart' and 'positionEnd'. The code doing this is a pretty simple and basic animation:
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"position"];
theAnimation.duration=1.0;
theAnimation.beginTime=CACurrentMediaTime()+1;
theAnimation.repeatCount=HUGE_VALF;
theAnimation.autoreverses=YES;
theAnimation.fromValue=[NSValue valueWithCGPoint:positionStart];
theAnimation.toValue=[NSValue valueWithCGPoint:positionEnd];
[self.pulseLayer addAnimation:theAnimation forKey:@"animatePosition"];
Now here's the question: I want to have this section of code send a message to another object with every "tick" of this clock; that is, every time pulseLayer's presented position goes to 'positionStart' I want this section of code to send a target-action message to another object (let's call it Clock #2) which I want to keep in sync with the periodic bouncing of the 'pulseLayer'.
Is there any simple way of doing this? The best alternative ideas I could come up with are (1) to start an NSTimer at the same time as the start of this animation to send "tick" timing pulses to Clock #2, but I worry that although the NSTimer object and the 'pulseLayer' bounce may start off in sync that small timing errors may build up over time so that they will no longer appear to be in sync after a long time. There would certainly be nothing to force them to remain synchronized. Another idea (2) is to do away with this endless bouncing of pulseLayer (i.e., change theAnimation.repeatCount to equal 0) and have another object send timing pulses to start the one-bounce animation every 2 seconds to both this object and to the "Clock #2" object that I want to keep in sync.
Any ideas about the best way to implement what I want to do here?