0

我已经为“滑块解锁”元素创建了一个 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{
                ...
            }
        }
    }
 }

但是动画不能以这种方式工作。有人知道为什么吗?

4

2 回答 2

0

您可以尝试将[UIView beginAnimations]and[UIView commitAnimations]放在循环之外。

如果这不起作用,您可以尝试在视图层上使用CoreAnimations

于 2013-08-23T10:48:50.133 回答
0

您应该使用更新的基于块的动画 API 来控制持续时间,同时设置滑块的值:

[UIView animateWithDuration:2 animations:^{
    [slider setValue:1];
}

此外,您有 2 个不同的循环,但您可能应该只有 1 个。您需要一个循环来创建所有滑块并将它们添加到视图中。但是,当您从其中一个滑块获得回调时,您不需要循环。相反,您应该只与调用该操作的滑块(即用户正在与之交互的滑块)进行交互。

于 2013-08-23T11:01:58.513 回答