Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPVolumeView animation issue

Every time I add MPVolumeView as a subview to my UIViewController’s view, there is a quick animation (the MPVolumeView expands from left to right) which looks really weird. I’m looking for a way to get rid of this animation, has anyone faced this issue?

I almost accepted that this is a MPVolumeView bug but then I noticed that Apple is definitely using a MPVolumeView in their native music app, no weird animations there... So there must be something I'm doing wrong.

UPDATE:

The code is pretty straightforward but it was requested in the comments, so here it is:

MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(10.f, 0.f, CGRectGetWidth(self.view.frame) - 20.f, 30.f)];
[[UISlider appearanceWhenContainedIn:[MPVolumeView class], nil] setMinimumValueImage:[UIImage imageNamed:@"icon-volumeMin"]];
[[UISlider appearanceWhenContainedIn:[MPVolumeView class], nil] setMaximumValueImage:[UIImage imageNamed:@"icon-volumeMax"]];
volumeView.center = CGPointMake(0.5f * CGRectGetWidth(self.view.frame), 0.5f * CGRectGetHeight(self.view.frame));
volumeView.showsRouteButton = NO;
[self.view addSubview:volumeView];

I made a very simple project on github to demostrate the problem, but you have to run it on a device, since MPVolumeView does not show up on simulator. Or just take a look at this gif:

gif:

like image 857
dariaa Avatar asked Jan 10 '15 16:01

dariaa


1 Answers

One possible way to remove this behavior is to subclass MPVolumeView and perform some additional work after [super layoutSubviews].

- (void)layoutSubviews
{
    [super layoutSubviews];

    [self xy_recursiveRemoveAnimationsOnView:self];
}

- (void)xy_recursiveRemoveAnimationsOnView:(UIView *)view
{
    [view.layer removeAllAnimations];
    for (UIView *subview in view.subviews) {
        [self xy_recursiveRemoveAnimationsOnView:subview];
    }
}

This removes all inserted animations. So be sure that is what you want, since this is quite the overkill. One could also just remove the position and bounds animations (see removeAnimationForKey:).

like image 92
Patrik Avatar answered Oct 26 '22 18:10

Patrik