iphone - stop and restore method
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;}
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;
精彩评论