iphone - 停止和恢复方法

时间:2009-12-08 02:53:22

标签: iphone

我正在使用此功能创建动作,我该如何停止动作?我希望它恢复到起点。感谢。

[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;}

1 个答案:

答案 0 :(得分:2)

在某些时候,您应该使计时器无效。您需要存储对它的引用才能执行此操作:

在您的标头文件中:

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

在您的实施文件中:

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

稍后点:

[self.timer invalidate];
self.timer = nil;         //very important, to avoid dangling pointers
jb.position = self.originalPoint;