我只是在徘徊,如果有更简单的方法来重复下面的代码 20 秒。如果有,怎么办?
[self performSelector:@selector( move1) withObject:nil afterDelay:0.0];
[self performSelector:@selector( move2) withObject:nil afterDelay:0.2];
[self performSelector:@selector( move3) withObject:nil afterDelay:0.4];
[self performSelector:@selector( move1) withObject:nil afterDelay:0.8];
[self performSelector:@selector( move2) withObject:nil afterDelay:0.10];
[self performSelector:@selector( move3) withObject:nil afterDelay:0.12];
最佳答案
根据我的意见,只需尝试下面的代码,
在你的 Controller 的 .h 文件中取一个 NSInteger,像这样,
NSInteger intTmp;
然后在.m文件中这样调用NSTimer方法,
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(testMethod:) userInfo:nil repeats:YES];
然后这样写选择器
-(void)testMethod:(NSTimer *)pTmpTimer
{
intTmp += 1;
if(intTmp <= 20)
{
[self performSelector:@selector( move1) withObject:nil afterDelay:0.0];
[self performSelector:@selector( move2) withObject:nil afterDelay:0.2];
[self performSelector:@selector( move3) withObject:nil afterDelay:0.4];
[self performSelector:@selector( move1) withObject:nil afterDelay:0.8];
[self performSelector:@selector( move2) withObject:nil afterDelay:0.10];
[self performSelector:@selector( move3) withObject:nil afterDelay:0.12];
}
else
{
[pTmpTimer invalidate];
intTmp = 0;
}
}
从上面的代码中,testMethod 将调用 20 次,根据您的要求,您的代码将重复 20 次..
希望它对你有用。
快乐的编码..
关于iphone - 重复 self 执行选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11409113/