iphone - 重复 self 执行选择器

标签 iphone ios xcode

我只是在徘徊,如果有更简单的方法来重复下面的代码 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/

相关文章:

ios - 从 Objective-C 转换为 Swift (TestFlight)

ios - 循环期间更新进度条

iphone - performSelectorOnMainThread 不执行

ios - 奇怪的 NSManagedObject 行为

ios - 如何从具有多个数组的字典中获取特定键并将其存储到放置在 TableView 单元格中的字符串中

ios - 在适合的 ScrollView 中显示各种表格 View

iOS:测量通话质量/信号强度

iphone - 如何 "pass on"可变数量的参数到 NSString 的 +stringWithFormat :

ios - 统一2D : after-image from OLED screens in a high contrast situation

c++ - Mac OSX - Xcode/泄漏问题