One requirement recently is to control the volume of the system. We all know that the principle is in the mediaplayer. Framework framework.
<span style="font-size:18px;">// The current volume of playing music, in the range of 0.0 to 1.0.// This property is deprecated -- use MPVolumeView for volume control instead.@property(nonatomic) float volume NS_DEPRECATED_IOS(3_0, 7_0);[[MPMusicPlayerController applicationMusicPlayer] setVolume:self.volumeSlider.value];</span>
But in fact, it was abandoned after ios7. The official saying is that we want to replace mpvolumeview. well, now there is a problem: mpvolumeview is difficult to customize. He can only customize some images. If we want to use gestures to control the volume up and down, how can we do it?
<span style="font-size:18px;">MPVolumeView *volumeView = [[MPVolumeView alloc] init]; [self.view addSubview:volumeView]; [volumeView sizeToFit]; NSLog(@"%@",volumeView.subviews);</span>
Print his subviews and you will find
There is a class called mpvolumeslider, so we only need to be able to control this class, but it cannot be implemented if we force create this class, but it doesn't matter, its baseclass is uislider. we can implement it using this method.
<span style="font-size:18px;">self.slider = [[UISlider alloc]init]; self.slider.backgroundColor = [UIColor blueColor]; for (UIControl *view in volumeView.subviews) { if ([view.superclass isSubclassOfClass:[UISlider class]]) { NSLog(@"1"); self.slider = (UISlider *)view; } } self.slider.autoresizesSubviews = NO; self.slider.autoresizingMask = UIViewAutoresizingNone; [self.view addSubview:self.slider];</span>
At this time, you will also find that the frame is (0, 0, 35, 34), then you will find that your equal slider is always in the upper left corner of the screen, my solution to this problem is to directly hidden = yes, and then re-establish a slider to associate with its value, and then rewrite the touch event, so that you can directly control it through gestures. the complete code is as follows:
<span style="font-size:18px;">#import "ViewController.h"@interface ViewController ()@property (nonatomic,strong)UISlider *volumeSlider;@property (nonatomic,strong)UISlider *slider;@property (nonatomic,assign)CGPoint firstPoint;@property (nonatomic,assign)CGPoint secondPoint;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. MPVolumeView *volumeView = [[MPVolumeView alloc] init]; [self.view addSubview:volumeView]; [volumeView sizeToFit]; NSLog(@"%@",volumeView.subviews); self.slider = [[UISlider alloc]init]; self.slider.backgroundColor = [UIColor blueColor]; for (UIControl *view in volumeView.subviews) { if ([view.superclass isSubclassOfClass:[UISlider class]]) { NSLog(@"1"); self.slider = (UISlider *)view; } } self.slider.autoresizesSubviews = NO; self.slider.autoresizingMask = UIViewAutoresizingNone; [self.view addSubview:self.slider]; self.slider.hidden = YES; NSLog(@"%f",self.slider.value);// UISlider *slider = // self.volumeSlider = [[UISlider alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];// self.volumeSlider.backgroundColor = [UIColor yellowColor];// self.volumeSlider.minimumValue = 0.0;// self.volumeSlider.maximumValue = 1.0;// self.volumeSlider.continuous = YES;// [self.volumeSlider addTarget:self action:@selector(volumeChange) forControlEvents:UIControlEventValueChanged];// [self.view addSubview:self.volumeSlider]; UISlider *slider1 = [[UISlider alloc] initWithFrame:CGRectMake(0, 100, 200, 20)]; slider1.tag = 1000; slider1.minimumValue = self.slider.minimumValue; slider1.maximumValue = self.slider.maximumValue; slider1.value = self.slider.value; [slider1 addTarget:self action:@selector(updateValue:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:slider1];}- (void)updateValue:(UISlider *)slider{ self.slider.value = slider.value;}- (void)volumeChange{ [[MPMusicPlayerController applicationMusicPlayer] setVolume:self.volumeSlider.value]; }- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ for(UITouch *touch in event.allTouches) { self.firstPoint = [touch locationInView:self.view]; } UISlider *slider = (UISlider *)[self.view viewWithTag:1000]; slider.value = self.slider.value; NSLog(@"touchesBegan");}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ for(UITouch *touch in event.allTouches) { self.secondPoint = [touch locationInView:self.view]; } NSLog(@"firstPoint==%f || secondPoint===%f",self.firstPoint.y,self.secondPoint.y); NSLog(@"first-second==%f",self.firstPoint.y - self.secondPoint.y); self.slider.value += (self.firstPoint.y - self.secondPoint.y)/500.0; UISlider *slider = (UISlider *)[self.view viewWithTag:1000]; slider.value = self.slider.value; NSLog(@"value == %f",self.slider.value); self.firstPoint = self.secondPoint;}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"touchesEnded"); self.firstPoint = self.secondPoint = CGPointZero;}</span>
Source code download: http://download.csdn.net/detail/woshiwls/7548545