2012-05-28 26 views
1

我有一组滑块,我使用Value Changed将数字提供给%指标。我也使用这个值来检查滑块是否低于某个点。如果是这样,我想运行一个UIViewAnimation(我是,它都工作正常)。但是,如果滑块移动到阈值以下,则动画会不断调用,这意味着正在动画的项目从点a移动到点b,然后一遍又一遍地返回。那么,我只能在阈值点触发一次动画吗?iOS,在滑块值n处触发UIViewAnimation一次?

这是我如何得到我的像素值:

_sizeSliderRange = _sizeSlider.frame.size.width - _sizeSlider.currentThumbImage.size.width; 
_sizeSliderOrigin = _sizeSlider.frame.origin.x + (_sizeSlider.currentThumbImage.size.width/4.0); 
_sizeSliderValueToPixels = (_sizeSlider.value * _sizeSliderRange) + _sizeSliderOrigin; 

我使用条件改变IBAction为功能链接的价值里面checkt他的价值和做的工作:

if (_sizeThumbX < 85) { //if within 60px of the left margin we animate the label to sit float left 
    [UIView transitionWithView:_sizeLabel duration:0.25f options:UIViewAnimationCurveEaseInOut animations:^(void) { etc etc 

谢谢。

+2

只需使用一个标志来检查动画是否已经运行一次。 – Pochi

回答

1

像@Luis说,只使用一个BOOL属性是这样的:

if (_sizeThumbX < 85) { //if within 60px of the left margin we animate the label to sit float left 
    if (!self.passedBelowThreshold) { 
     [UIView transitionWithView:_sizeLabel duration:0.25f options:UIViewAnimationCurveEaseInOut animations:^(void) { /* ... */ } 
    } 
} 
self.passedBelowThreshold = _sizeThumbX < 85; 
+0

非常好,谢谢。 – mrEmpty

1

你的代码是按照你的逻辑是工作每次滑块值改变,它是低于85的动画将被调用。你可以有动画在follwing方式触发一次: -

1>你可以保持绝对值在该动画occurs.Something像_sizeThumbX == 85

2>或你canhave多少倍的价值计数器变化 。在不同的函数中计数并存储滑块变化的值。如果滑块值位于85范围内,则不要增加计数器值,并在动画部分检查计数器标志和滑块的当前位置(如果滑块仍在85范围内)如果计数器值已经是1,则不调用动画,即动画已经被触发,否则调用并增加动画计数器。

3>我不是你的条件,因为你没有清楚地提到,但是如果你想要再次调用动画,如果你的滑块超出范围并再次返回,在这种情况下,使计数为零(滑块越过指定的范围)。

+0

你好。以为我有这个,但没有。我正在使用: 'int thumbCount = 0;如果(_sizeThumbX <85){(thumbCount == 0){ thumbCount ++; NSLog(@“Thumb count is:%d”,thumbCount); NSLog (UIView TransitionWithView:_sizeLabel duration:0.5f options:UIViewAnimationCurveEaseInOut animations:^(void){_sizeRightLabel.transform = CGAffineTransformMakeTranslation(_sizeThumbX - 180,0); }完成:^(BOOL finished){}]; }; } else { };' – mrEmpty

+0

我正在使用bool来跟踪动画是否已经触发,日志显示bool正在给出我期望的值,但无论如何都会触发动画。 – mrEmpty