iOS 에서 화면 밝기 와 시스템 볼 륨 을 제어 합 니 다.

그림 에서 보 듯 이 왼쪽 은 위아래 로 미 끄 러 지 며 밝 기 를 바 꾸 고 오른쪽 은 위아래 로 미 끄 러 지 며 볼 륨 을 바꾼다.
这里写图片描述
1.화면 밝기 변경

 //        
  light = [UIScreen mainScreen].brightness;
  light = 0.5f;
  //        set    
  [UIScreen mainScreen].brightness = light;
2.시스템 볼 륨 변경
많이 사용 되 는 것 은 MPMusicPlayerController 를 통 해 시스템 볼 륨 을 바 꾸 는 것 으로 주로 MPMusicPlayerController 의 볼 륨 과 시스템 의 동기 화 덕분이다.

MPMusicPlayerController *mpVC = [MPMusicPlayerController applicationMusicPlayer];
//        
voice = _mpVC.volume;
voice = 0.5f;
//      
_mpVC.volume = voice;
3.미끄럼 응답 방법

- (void)leftSwipGes:(UIPanGestureRecognizer *)leftSwip
{
  //            y  
  leftCurrentY = [leftSwip translationInView:leftView].y;
  NSLog(@"left");
  //              ,     
  if (leftCurrentY > leftLastY) {
    if (light > 0) {
      //        ,    
      light = light - 0.01;
    }
    else{
      //    0    0,     
      light = 0;
    }
  }
  else
  {
    if (light < 1) {
      //        
      light = light + 0.01;
    }
    else{
      //    1    1,    1
      light = 1;
    }
  }
  //      
  [UIScreen mainScreen].brightness = light;
  //             y  
  leftLastY = leftCurrentY;
}
//          ,      
- (void)rightSwipGes:(UIPanGestureRecognizer *)rightSwip
{
  NSLog(@"right");
  rightCurrentY = [rightSwip translationInView:rightView].y;
  if (rightCurrentY > rightLastY) {
    if (voice > 0) {
      voice = voice - 0.01;
    }
    else{
      voice = 0;
    }
  }
  else
  {
    if (voice < 1) {
      voice = voice + 0.01;
    }
    else{
      voice = 1;
    }
  }
  //      
  _mpVC.volume = voice;
  rightLastY = rightCurrentY;
}
4.AVAudioPlayer 단순 활용

// budle         ,            ,mp3       
  NSString *string = [[NSBundle mainBundle] pathForResource:@"       " ofType:@"mp3"];
  NSString *playUrl = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //   UTF-8          
  //        url  
  NSURL *url = [NSURL URLWithString:playUrl];
  //               
  _avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
  [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
  //        
  _avAudioPlayer.volume = voice;
  //         -1     
  _avAudioPlayer.numberOfLoops = -1;
  //   
  [_avAudioPlayer prepareToPlay];
  [_avAudioPlayer play];
여기에 큰 구덩이 가 있 으 니 반드시 배경 재생 을 설정 해 야 한다.그렇지 않 으 면 재생 되 지 않 을 것 이다.아래 코드 는

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
마지막 으로 데모 첨부:다운로드 클릭
위 에서 설명 한 것 은 편집장 이 소개 한 iOS 에서 화면 밝기 와 시스템 볼 륨(AVAudio Player 기본 용법 과 Masonry 를 추가 하여 간단하게 사용)을 미 끄 러 뜨리 는 것 입 니 다.여러분 께 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남 겨 주세요.편집장 은 바로 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기