首页 文章

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

提问于
浏览
7

根据播放的音频,我有一个滑块可以作为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;}
    ];
}];

这在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;}];

将同时移动滑块及其值 together ,两者都是动画的

[UIView animateWithDuration:0.3
                 animations:^{self.volumeSlider.frame = sliderFrame;}];
    [UIView animateWithDuration:0.3
                 animations:^{self.volumeSlider.value = newValue;}];

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

  • 我尝试通过调用第二个动画
[self performSelector:@selector(animateVolume:) withObject:nil afterDelay:0.3];

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

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

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

并在第一个动画结束后 -

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

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

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

谢谢:)丹

UPDATE

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

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

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

[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;
}];

1 回答

  • 7

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

相关问题