在iOS 7上跳过了UISlider中的动画

Dan*_*Dan 7 xcode cocoa-touch uiview uiviewanimation ios7

根据播放的音频,我有一个滑块可以作为2个滑块使用 - 当禁用1种类型的音频(某种声乐指导),播放音乐,滑块控制音乐的音量.

更改角色时,滑块会根据其角色(引导 - 视图中的上方,音乐 - 下方)更改位置,并将其值(音量)调整为该类型声音(引导声音或音乐声音)的已保存音量值).

我正在寻找的效果类型是 -

  • 使用滑块将滑块移动到新位置 [UIView animateWithDuration]
  • 当滑块到达其位置时,再次使用更改其值以反映音量[UIView animateWithDuration].

首先,我是这样写的 -

[UIView animateWithDuration:0.3
    animations:^{self.volumeSlider.frame = sliderFrame;}
    completion:^(BOOL finished){
        [UIView animateWithDuration:0.3
            animations:^{self.volumeSlider.value = newValue;}
    ];
}];
Run Code Online (Sandbox Code Playgroud)

这在iOS 6模拟器中运行得非常好(使用Xcode 4.6.3),但是当更改到我的手机,运行iOS 7时,滑块改变了它的位置,然后滑块的值跳转到新值.在Xcode 5附带的iOS 7模拟器中运行时再次出现同样的问题,所以我认为这是一个iOS 7问题.

我做了一些实验,结果不同:

  • 我尝试使用'[UIView animateWithDuration:0.3 delay:0.3 options:animations:completion:]'来设置音量,这意味着,不是在完成部分,但是同样的事情发生了.
  • 当一个接一个地放置2个动画(每个动画作为单独的动画,每个动画没有延迟,一个接一个)时,结果将根据动画的顺序而变化.

    [UIView animateWithDuration:0.3 animations:^{self.volumeSlider.value = newValue;}];
    [UIView animateWithDuration:0.3 animations:^{self.volumeSlider.frame = sliderFrame;}];
    
    Run Code Online (Sandbox Code Playgroud)

将滑块及其值同时移动,同时动画

    [UIView animateWithDuration:0.3
                 animations:^{self.volumeSlider.frame = sliderFrame;}];
    [UIView animateWithDuration:0.3
                 animations:^{self.volumeSlider.value = newValue;}];
Run Code Online (Sandbox Code Playgroud)

将移动滑块的位置,然后在没有动画的情况下更改其值.

再次 - 滑块移动,然后立即更改值.

为什么,OH为什么?如果它有帮助,这是滑块的描述,在第一个动画之前 -

<UISlider: 0xcc860d0; frame = (23 156; 276 35); autoresize = RM+BM;
layer = <CALayer: 0xcc86a10>; value: 1.000000>
Run Code Online (Sandbox Code Playgroud)

并在第一个动画结束后 -

<UISlider: 0xcc860d0; frame = (23 78; 276 35); autoresize = RM+BM; 
animations = { position=<CABasicAnimation: 0xbce5390>; }; 
layer = <CALayer: 0xcc86a10>; value: 0.000000>
Run Code Online (Sandbox Code Playgroud)

注意动画部分,现在不应该在那里(描述是从[self animateVolume]记录的,它被调用,延迟为.3秒).

我知道它有一个奇怪的问题,但我非常感谢它的帮助.

谢谢:)丹

UPDATE

正如Christopher Mann所说,改变UIView中的值:animationWithDuration不是使用它的官方方式,正确的方法是使用UISlider的setValue:animated.

然而,对于将来会遇到这样的问题的人 - 似乎iOS 7在该方法上有一些困难,因此在某些情况下它没有动画(我认为如果项目是在Xcode中启动的话它将不会动画<5).这里描述问题及其解决方案.

我解决这个问题的代码是:

[UIView animateWithDuration:0.3
                 animations:^{self.volumeSlider.frame = sliderFrame;}
                 completion:^(BOOL finished){
                     [UIView animateWithDuration:1.0 animations:^{
                         [self.volumeSlider setValue:newValue animated:YES];
                     }];
                     currentSliderMode = mode;
}];
Run Code Online (Sandbox Code Playgroud)

小智 7

如果要为滑块值的更改设置动画,则应使用setValue:animated:而不是直接设置.value.更改UIView动画块内的volumeSlider.value可能会干扰动画.

  • 看起来你是对的,虽然使用setValue:animated对于一个完全不同的bug没有用,如下所述:http://stackoverflow.com/questions/19055645/uislider-not-animating-in-ios7.最后,我使用了你所建议的解决方法.你可以在我的更新中看到.非常感谢您的帮助 :) (2认同)