Xcode4 如何播放一个计时器并跳转到另一个计时器,然后作为循环返回?

发布于 2024-11-29 14:10:16 字数 1953 浏览 2 评论 0原文

然而,它有点复杂...... 我需要两个 NSTimer:

  1. myTimer1 的速度为 1 秒。间隔并玩 6 间隔停止并跳转到 myTimer2...
  2. myTimer2 的速度为 2 秒。间隔并玩 6 间隔,然后跳回 myTimer1
  3. 并继续此操作,直到触摸停止按钮。

我已经设法只让一个计时器发挥作用,并且可以弄清楚如何完成其​​余的事情。 这是我使用的代码概要: .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:) 

userInfo:nil Repeats:YES];}

所以这是我一直在使用的公式,在我添加“if”语句之前它起作用了并且两个彩色 UIView 交替得很好。当我将 myTimer1 中的速度更改为 2 时,它可以更改速度间隔。我只需要帮助使用工作语法来添加第二个计时器跳转,然后循环它。

甚至可能无法做到这一点,但我希望可以,并且有人可以帮助我。

非常感谢。

- 抢

It's a bit more complex however...
I need two NSTimers:

  1. myTimer1 will have a speed of 1 sec. intervals and play for 6
    intervals stop and jump to myTimer2...
  2. myTimer2 will have a speed of 2 sec. intervals and play for 6
    intervals and then jump back to myTimer1
  3. and continue this until a stop button is touched.

I have managed to only get the one timer to play and can figure out how to do the rest.
Here is the code synopsis I used:
.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:) 

userInfo:nil repeats:YES];}

So this is the formula I've been playing with and before I added the "if" statement it worked and the two colored UIViews alternated fine. When I change the speed to two in myTimer1 to 2 that works in changing the speed interval. I just need help using a working syntax to add the second timer jump and then looping it.

It may not even be possible to do this, but I hope it is and someone can help me.

Many thanks.

--Rob

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可可 2024-12-06 14:10:16

我会创建两个 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;

I would create Two NSStrings:

NSString *countone
NSString *countwo

Then in your selector for your first timer:

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

and so on until you get to:

    else if ([countone isEqualToString:@"6"]) {
     //stop your first timer
     //start your second timer
    countone = @"";
}

And you can do exactly the same for your second timer, just the orher way round so that at the end you start your first timer, and like that it loops over and over!
And to stop the timer do:

[Timer invalidate];
Timer=nil;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文