UIView animateWithDuration not working with UISlid

2019-09-14 19:38发布

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.

2条回答
相关推荐>>
2楼-- · 2019-09-14 20:16

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.

查看更多
冷血范
3楼-- · 2019-09-14 20:20

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

查看更多
登录 后发表回答