0

I have started a new IOS app and I have a problem in my code. Description about the app: At the middle of the screen there placed a circle, when I tap the screen the circle starting move to the point that i tapped there. I have program it and its work very well, but there is one more thing: when i tap in another point on the screen so the circle will move to the second point right after i tapped there! - even if the circle still moving to the first point, immediately he will start move to the second point. (and this is the hard part).

here is my code when I tap the screen:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view.layer removeAllAnimations];

    UITouch *myTouch = [[touches allObjects] objectAtIndex: 0];
    CGPoint currentPos = [myTouch locationInView: self.view];

    [UIView animateWithDuration:2
                              delay:0.0f
                            options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat |    UIViewAnimationOptionAutoreverse
                         animations:^(){
                             circle.center = CGPointMake(currentPos.x, currentPos.y);
                         }
                         completion:^(BOOL finished){}];

  }

Please help me to solve that problem.. maybe there is another way to animate that circle or when tapping the screen.

even if you can give code that when I press a button so the circle just will stop and dont move right at the point that he was when i pressed the button - (even if the circle was in animation) so it will be great.. Thanks.

Nir
  • 25
  • 6

1 Answers1

0

From what I know there is no value referenced in UIViewAnimation that holds the current center of the animation so you will need to write your own animation algorithm.

The problem about "stopping the circle" is that the circle position is not recorded anywhere that we can access inside UIViewAnimation. In order to see what I mean in action try to NSLog() the circle.center point and you will see that when you tap the screen the value of circle.center will never change until you move the position you tap on the screen. (Even though it is constantly moving the circle.center CGPoint doesn't change)

That means that the animation is setting the center of the circle near-instantly and when the animation is "stopped" it doesn't stop where it is, it stops when the circle's center was set when the animation began.

I know this isn't what you wanted to hear but it will be more work than you anticipated to make this program the way you'd like. I'm not familiar with custom animations in Core Animation but here are a few links to help get you started.

A question similar to yours on another website.

How to find offsets between two points (In your case the starting animation and ending)

You will also want to read up on Pythagorean Theory and NSTimeInterval so that you can calculate the current position of where the animation is at.

Community
  • 1
  • 1
Scott
  • 1,154
  • 1
  • 12
  • 25
  • I think you dont understand me, you know what? even if you can give code that when I press a button so the circle just will stop and dont move right at the point that he was when i pressed the button - (even if the circle was in animation) so it will be great.. – Nir Nov 24 '14 at 20:50
  • @Nir The way I understood is you want the circle to complete animating until you can tap again to make a circle in a new place. Is that correct? – Scott Nov 24 '14 at 20:52
  • The circle its just a character/object on the screen that follow's my taps with animated movements.. – Nir Nov 24 '14 at 20:58
  • @Nir I understand that part. Do you want the circle to complete animating before the next tap can be registered/animated? – Scott Nov 24 '14 at 21:06
  • No man, the circle doesnt need complte the first animation, he need immediately run the second animation.. – Nir Nov 24 '14 at 21:11
  • @Nir Gotcha, what you could do is calculate the offset between the beginning CGPoint and the ending CGPoint and when the user presses the screen you would call a method that pending on the time spent between the start of the animation determines the current position of the circle. (Since the circle position is set to the point you tapped right away) But that would be a lot of math. I'll do some research to see if something stores the current position of the animation center point while it's animating which would be much easier to implement. – Scott Nov 24 '14 at 21:36
  • Yes its can be help, but first of all - how to stop the circle animation before he complete the animation? if you will find out something i will very thankful! – Nir Nov 24 '14 at 21:40
  • @Nir I've edited my answer to give you more information. Hopefully it helps a bit on clarifying the animations and positions. – Scott Nov 24 '14 at 22:06