在 iOS 8 中存在一个问题或者说一个特性。当 MPVolumeView 被展示时,它会被动画化,就像从0到其宽度的展开过程。如何解决这个问题?在 iOS 7 中并不存在这样的问题。
消除这种行为的一种可能的方法是继承MPVolumeView,并在[super layoutSubviews]
之后执行一些额外的工作。
- (void)layoutSubviews
{
[super layoutSubviews];
[self cg_recursiveRemoveAnimationsOnView:self];
}
- (void)cg_recursiveRemoveAnimationsOnView:(UIView *)view
{
[view.layer removeAllAnimations];
for (UIView *subview in view.subviews) {
[self cg_recursiveRemoveAnimationsOnView:subview];
}
}
这将删除所有插入的动画。因此,请确保您想要这样做,因为这是相当过度的。也可以只删除position
和bounds
动画(参见removeAnimationForKey:
)。
UIView.performWithoutAnimation
- 更简单。 - Léo Natan-performWithoutAnimation
包装[super layoutSubviews]
吗?这并没有起作用,因为动画似乎是在别处创建的,并且MPVolumeView
中并不是每个图层都有一个UIView
代理。 - PatrikperformWithoutAnimation:
,它将被丢弃。如果未被丢弃,则表示其要么是在之前排队的,要么是 CA 直接动画。 - Léo Natan我确认这个问题在iOS 8中仍然存在。Anastasia在上面的评论中提供的解决方法(通过覆盖volumeSliderRectForBounds)似乎可以工作,但只有当路由按钮不存在时才有效。当它存在时,滑块会重叠在路由按钮上,无法再次按下。
我对她的解决方案进行了简单的修改,也许有人可以将其用作解决方法,直到苹果修复该问题或提供更好的解决方案。
- (CGRect)volumeSliderRectForBounds:(CGRect)bounds
{
if (self.showsRouteButton) {
NSInteger spacer = 10; /* Space between Route button and Volume slider */
CGRect routeButtonRect = [self routeButtonRectForBounds:bounds];
bounds.size.width -= (routeButtonRect.size.width + spacer);
}
return bounds;
}
我不喜欢硬编码间距值,但我找不到如何动态计算它的方法。
- (CGRect)volumeSliderRectForBounds:(CGRect)bounds { return bounds; }
。不幸的是,这只是一个解决方法。我没有检查如果需要显示路由按钮(例如AirPlay)它是如何工作的。另一方面,自iOS7以来,在应用程序中Apple不再显示此按钮,因为它在控制中心中已经展示,所以我也不再显示它了。对我而言,这种解决方案非常有效。 - Anastasia