Problem with MPVolumeView animation

Every time I add MPVolumeView as a subtitle to my UIViewController , there is a quick animation ( MPVolumeView extension MPVolumeView left to right) that looks really weird. I am looking for a way to get rid of this animation, has anyone encountered this problem?

I almost agreed that it was an MPVolumeView error, but then I noticed that Apple definitely uses MPVolumeView in its native music application, there are no weird animations ... So there must be something that I am doing wrong.

UPDATE:

The code is pretty simple, 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 demonstrate the problem, but you need to run it on the device, since MPVolumeView does not display on the simulator. Or just take a look at this gif:

gif :

+5
source share
2 answers

One possible way to eliminate this behavior is to subclass MPVolumeView and do extra 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]; } } 

Deletes all inserted animations. Therefore, be sure that this is what you want, as this is pretty much overkill. You can also simply remove the position and bounds animations (see removeAnimationForKey: .

+10
source

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 must make layout calls in viewWillLayoutSubviews.

Instead of creating a new MPVolumeView every time you click a button, you should create it in viewDidLoad and hide it, and then display it when you click the button.

-1
source

Source: https://habr.com/ru/post/1210806/


All Articles