我为“滑块解锁”元素创建了一个UISlider。我写的动画只指向一个滑块时效果很好。但我需要使用for循环动态创建滑块。当我为滑块创建滑块添加标签并将动画功能添加到for循环中的滑块时,动画不起作用,如下所示:
for (int i = 0; i < rowCount; i++){
...
UISlider *slider=[[UISlider alloc] initWithFrame:CGRectMake(5+ xInc, 25+yInc, 322, 40)];
//give a id tag to the slider
[slider setTag:i*10];
//set a action to the slider
[slider addTarget:self action:@selector(UnlocklinkSlider:) forControlEvents:UIControlEventTouchUpInside];
...
动画功能是:
- (void)UnlocklinkSlider:(UISlider*)slider
{
for (int i = 0; i < rownumber; i++){
UIImageView *frontimg = (UIImageView *)[self.view viewWithTag:i*10+1];
...
if (slider.tag==i*10) {
if(slider.value >= 0.5){
// user did not slide far enough, so return back to 0 position
[UIView beginAnimations: @"SlideCanceled" context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.35];
// use CurveEaseOut to create "spring" effect
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
...
[UIView commitAnimations];
...
}
else{
...
}
}
}
}
但动画不能以这种方式运作。有人知道为什么吗?
答案 0 :(得分:0)
您可以尝试将[UIView beginAnimations]
和[UIView commitAnimations]
放在循环之外。
如果这不起作用,您可以尝试在视图图层上使用CoreAnimations。
答案 1 :(得分:0)
您应该使用较新的基于块的动画API来控制持续时间,同时设置滑块的值:
[UIView animateWithDuration:2 animations:^{
[slider setValue:1];
}
此外,你有2个不同的循环,但你可能只有1个。你想要一个循环来创建所有的滑块并将它们添加到视图中。但是,当你从其中一个滑块获得回调时,你不需要循环。相反,您应该只与调用操作的滑块(即用户正在与之交互的滑块)进行交互。