因此,目前我在UIViewcontroller中有一个UISlider,用于在用户滑动时在子视图中启动动画 . 基本上当用户滑动时,我将这个电池充满,用空格填充空电池图像以指示内部电源一个单元,用户可以滑动以查看电池在一天中的某些时间所具有的能量 .
目前,当View加载时,我希望UISlider自动从滑块的开始滑动并滚动到结束,比如5秒 .
我实现了一个循环,循环遍历uislider的所有值使用此循环
for (int i = 0; i < [anObject count] - 2; i++)
{
sleep(.25);
NSUInteger index = (NSUInteger)(slider.value + 0.5); // Round the number.
[slider setValue:index animated:YES];
}
[anObject count] - 2
在一天中的这个时间等于62,但是每15秒就会改变并递增,因为我从服务器获取数据 .
但除此之外,为什么这不起作用?循环?
EDIT:
这就是我对NSTIMER的所作所为
[NSTimer timerWithTimeInterval:0.25 target:self selector:@selector(animateSlider) userInfo:nil repeats:NO];
和 animateSlider
看起来像这样:
- (void)animateSlider:(NSTimer *)timer
{
NSLog(@"Animating");
NSUInteger index = (NSUInteger)(slider.value + 0.5); // Round the number.
[slider setValue:index animated:YES];
}
但是没有运气......为什么不是NSTimer“解雇”.....我隐约知道有一种方法可以解决这个问题,但不确定是否需要......
EDIT:
啊它确实需要“火”......
NSTimer *timer = [NSTimer timerWithTimeInterval:0.25 target:self selector:@selector(animateSlider) userInfo:nil repeats:NO];
[timer fire];
但由于某种原因,它只会发射一次....任何想法?
1 回答
“出于某种原因,它只会发射一次......”
如果您更改了NSTimer设置为:
这将立即在当前运行循环上安排计时器 .
由于“重复”参数为“是”,因此您每四分钟重复一次计时器,直到您使计时器无效(当达到结束条件时应该这样做,就像滑块到达目的地时一样) .
附:你'd need to change the selector method declaration of your timer'的目标略有 . According to Apple's documentation,"The selector must correspond to a method that returns void and takes a single argument. The timer passes itself as the argument to this method."
所以请改为声明“
animateSlider
”: