UIView animateWithDuration not working with UISlider

北城余情 提交于 2019-12-11 19:37:34

问题


I want the slider to set value slowly and for that I am using this code

    [UIView animateWithDuration:2.0 animations:^{
         [self.slider setValue:(float)val];
    }];

But this is not working. Please help me out.


回答1:


Use [self.slider setValue:(float)val animated:YES]; instead.




回答2:


What I have found is that the animation will not work is the animation is scheduled while the view is still loading. So instead of this (which does not animate):

-(void)animateSlider: duration:(NSTimeInterval)duration
{
    _slider.maximumValue = duration;
    _slider.minimumValue = 0;
    [UIView animateWithDuration:duration
                        delay:0
                        options: UIViewAnimationOptionCurveLinear
                     animations:^{
                         [_slider setValue:duration animated:YES];
                     }
                     completion:^(BOOL finished){}
    ];
}

I do the following, where animateSlider is an internal method:

-(void)animateSlider
{
    float value = _slider.maximumValue;
    [UIView animateWithDuration:value
                        delay:0
                        options: UIViewAnimationOptionCurveLinear
                     animations:^{
                         [_slider setValue:value animated:YES];
                     }
                     completion:^(BOOL finished){}
    ];
}

-(void)animateSlider: duration:(NSTimeInterval)duration
{
    _slider.maximumValue = duration;
    _slider.minimumValue = 0;
     [self performSelector:@selector(animateSlider) withObject:nil afterDelay:0];
}

The afterDelay:0 means that the animation will be scheduled for the very next runloop.



来源:https://stackoverflow.com/questions/20539913/uiview-animatewithduration-not-working-with-uislider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!