Xcode4 如何播放一个计时器并跳转到另一个计时器,然后作为循环返回?
然而,它有点复杂...... 我需要两个 NSTimer:
- myTimer1 的速度为 1 秒。间隔并玩 6 间隔停止并跳转到 myTimer2...
- myTimer2 的速度为 2 秒。间隔并玩 6 间隔,然后跳回 myTimer1
- 并继续此操作,直到触摸停止按钮。
我已经设法只让一个计时器发挥作用,并且可以弄清楚如何完成其余的事情。 这是我使用的代码概要: .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:
- myTimer1 will have a speed of 1 sec. intervals and play for 6
intervals stop and jump to myTimer2... - myTimer2 will have a speed of 2 sec. intervals and play for 6
intervals and then jump back to myTimer1 - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会创建两个 NSStrings:
然后在你的第一个计时器的选择器中:
依此类推,直到你到达:
你可以为你的第二个计时器做完全相同的事情,只是反过来,这样最后你就可以启动你的第一个计时器,就这样一遍又一遍地循环!
要停止计时器,请执行以下操作:
I would create Two NSStrings:
Then in your selector for your first timer:
and so on until you get to:
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: