dariaa
dariaa

Reputation: 6385

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:

Upvotes: 6

Views: 1168

Answers (2)

Patrik
Patrik

Reputation: 1117

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:).

Upvotes: 10

peko
peko

Reputation: 11335

i fixed your demo-code

@implementation F17YViewController {
    MPVolumeView *volumeView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    volumeView = [[MPVolumeView alloc] init];
    volumeView.showsRouteButton = NO;
    volumeView.hidden = true;
    [[UISlider appearanceWhenContainedIn:[MPVolumeView class], nil] setMinimumValueImage:[UIImage imageNamed:@"icon-volumeMin"]];
    [[UISlider appearanceWhenContainedIn:[MPVolumeView class], nil] setMaximumValueImage:[UIImage imageNamed:@"icon-volumeMax"]];
    [self.view addSubview:volumeView];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

    volumeView.frame = CGRectMake(10.f, 0.f, CGRectGetWidth(self.view.frame) - 20.f, 30.f);
    volumeView.center = CGPointMake(0.5f * CGRectGetWidth(self.view.frame), 0.5f * CGRectGetHeight(self.view.frame));
}

- (IBAction)showVolumeView:(id)sender {
    volumeView.hidden = false;
}

You should do layout calls in viewWillLayoutSubviews.

Instead of creating a new MPVolumeView with each button press you should create one at viewDidLoad and hide it and then unhide when the button gets pressed.

Upvotes: -1

Related Questions