tags:

views:

73

answers:

1

hi, i'm using this function to create movement, how do i stop the movement? i want it to restore to the start point. thanks.

[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(shuffleOnTimer) userInfo:nil repeats:YES];

-(void) shuffleOnTimer {

    jb.center = CGPointMake(jb.center.x+pos1.x,jb.center.y+pos1.y);

    if(jb.center.x > 60 || jb.center.x < 0)
    pos1.x = -pos1.x;
    if(jb.center.y > 240 || jb.center.y < 100)
    pos1.y = -pos1.y;}
+2  A: 

At some point you should invalidate the timer. You'll need to store a reference to it in order to do this:

In your header file:

@class myClass : NSObject {
    ....
    NSTimer      *timer;
    CGPoint      originalPoint;
    ...
}
@property (nonatomic, readwrite, assign) NSTimer *timer;
@property (nonatomic, readwrite) CGPoint originalPoint;

In your implementation file:

self.originalPoint = jb.position;
self.timer = [NSTimer scheduledTimerWithTimeInterval…

as some later point:

[self.timer invalidate];
self.timer = nil;         //very important, to avoid dangling pointers
jb.position = self.originalPoint;
Ben Gottlieb
using self get compiler error (object cannot be set...)
omri
As he said, you'll need to store a reference to the timer. In his example, he's made the timer a property of the current class.
Brad Larson
now works but this only stop the animation, how do i restore it to the start position?
omri
When you call [self.timer invalidate], you can then set jb.center to the original position. Perhaps add an additional member variable to store the original position.
Ben Gottlieb
what to inert in the self.originalPoint = position; what is the position?btw why (readwrite, assign) in the property?
omri
Oops, forgot a word. Code has been updated.
Ben Gottlieb
THX.i used center instead position (jb.center), is that what you meant right?do i need to dealloc the timer object?why (readwrite, assign) for the timer property?do i need to create CGPoint (like originalPoint) for each of my objects (jb2 , jb3 ...)?
omri
timers are retained by the run loop when scheduled, so you don't have to retain it. When you invalidate it, the runloop will release it, so you nil it out there to prevent a dangling reference.
Ben Gottlieb
thanks!!! my last question, do i need to create CGPoint (like the originalPoint) for each of my objects (jb2 , jb3 ...) ?
omri
You'll need to store the original location for any object whose position you want to restore when the animation finishes.
Ben Gottlieb
is there a way to store more then one object location in one CGPoint? (and by so not creating 50 CGPoints for 50 image objects)
omri
no, but you could use an NSArray or NSDictionary to store your points in, using +[NSValue valueWithCGPoint:]
Ben Gottlieb
can you post an example?
omri
I think we've gotten a bit far afield from timers; you may want to post a new question.
Ben Gottlieb
already did, thanks a lot for your help.http://stackoverflow.com/questions/1871068/create-a-restore-point-current-position
omri