我正在使用这个函数来创建移动,我如何停止移动?我希望它恢复到起始点。谢谢。
[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;}发布于 2009-12-08 11:01:30
在某些情况下,您应该使计时器无效。为了做到这一点,你需要存储一个对它的引用:
在您的头文件中:
@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;https://stackoverflow.com/questions/1864282
复制相似问题