但它有点复杂...... 我需要两个NSTimers:
我设法只让一个计时器发挥,并可以弄清楚如何做其余的。 这是我使用的代码概要: .H
@interface sosTest8ViewController : UIViewController {
IBOutlet UILabel *myCounterLabel;
IBOutlet UIView *greenView;
IBOutlet UIView *blueView;
NSTimer *myTimer1;
NSTimer *myTimer2;
}
@property (nonatomic, retain)NSTimer *myTimer1;
@property (nonatomic, retain)NSTimer *myTimer2;
@property (nonatomic, retain) UILabel *myCounterLabel;
@property (nonatomic, retain) UIView *greenView;
@property (nonatomic, retain) UIView *blueView;
@end
的.m
@synthesize myCounterLabel;
@synthesize greenView;
@synthesize blueView;
@synthesize myTimer1;
@synthesize myTimer2;
- (void)viewDidLoad
{
[super viewDidLoad];
self.myCounterLabel.text = @"0";
myTimer1 = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(updateCounter:)
userInfo:nil
repeats:YES];
}
-(void)updateCounter:(NSTimer*)theTimer{
static int count = 1;
count++;
NSString *s = [[NSString alloc]initWithFormat:@"%d", count];
self.myCounterLabel.text = s;
[s release];
self.blueView.hidden = YES;
self.greenView.hidden = YES;
switch (count & 0x01) {//was & 0x03
case 0: self.blueView.hidden = NO; break;
case 1: self.greenView.hidden = NO; break;
}
//I added this if bit as my own way, but doesn't jump to the net timer properly
if (count >= 3) {
[myTimer1 invalidate];
self.myCounterLabel.text = @"0";
myTimer2 = [NSTimer scheduledTimerWithTimeInterval:2.0f
target:self
selector:@selector(updateCounter2:)
所以这是我一直在玩的公式,在我添加“if”语句之前,它的工作原理和两个颜色的UIViews交替正常。当我在myTimer1到2中将速度更改为2时,可以更改速度间隔。我只需要使用工作语法来帮助添加第二个定时器跳转然后循环它。
甚至可能无法做到这一点,但我希望是这样,有人可以帮助我。
非常感谢。
- 罗布
答案 0 :(得分:0)
我会创建两个NSStrings:
NSString *countone
NSString *countwo
然后在你的第一个计时器的选择器中:
if ([countone isEqualToString:@""]) {
//do your stuff
countone = @"1";
}
else if ([countone isEqualToString:@"1"]) {
//do stuff
countone = @"2";
}
else if ([countone isEqualToString:@"2"]) {
//do stuff
countone = @"3";
}
等等,直到你到达:
else if ([countone isEqualToString:@"6"]) {
//stop your first timer
//start your second timer
countone = @"";
}
你可以为你的第二个计时器做同样的事情,只是在另一个方向,所以最后你开始你的第一个计时器,就像它一遍又一遍地循环! 并停止计时器:
[Timer invalidate];
Timer=nil;