2015-01-06 71 views
0

好吧,讓我的應用程序設置在我的應用程序中,以便在單擊外部音量按鈕時進行閱讀。我的問題是,似乎我卡在這,我在我的應用程序兩個按鈕確定爲:iOS音量按鈕控制器應用程序按鈕

- (IBAction)sendMessage:(id)sender; 
- (IBAction)cancelMessage:(id)sender; 

當我點擊我的iPad向上^物理按鈕,我希望它與連接起來,

- (IBAction)sendMessage:(id)sender; 

然後用下來的物理按鈕,需要連接, - (IBAction)cancelMessage:(id)sender;

這裏是我的代碼,我是卡上:

#import "FirstViewController.h" 
#import "AppDelegate.h" 
#import "AudioToolbox/AudioServices.h" 
#import <MediaPlayer/MediaPlayer.h> 



@interface FirstViewController() 


@property (nonatomic, strong) AppDelegate *appDelegate; 

-(void)sendMyMessage; 
-(void)didReceiveDataWithNotification:(NSNotification *)notification; 

@end 

@implementation FirstViewController 

// ADDED THIS SECTION------------------------------------------------------------ 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

// ADDED THIS SECTION------------------------------------------------------------ 


int Count=0; 

- (void)viewDidLoad 
{ 
// ADDED THIS SECTION------------------------------------------------------------ 


    UISwitch *switcher = [[UISwitch alloc] init]; 
    switcher.center = self.view.center; 
    [switcher addTarget: self action: @selector(volumeListener_onOff:) forControlEvents:UIControlEventValueChanged]; 
    [self.view addSubview:switcher]; 

// ADDED THIS SECTION------------------------------------------------------------ 



    [super viewDidLoad]; 



    // Do any additional setup after loading the view, typically from a nib. 

    _appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    _txtMessage.delegate = self; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(didReceiveDataWithNotification:) 
               name:@"MCDidReceiveDataNotification" 
               object:nil]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 



// ADDED THIS SECTION------------------------------------------------------------ 
// ADDED THIS SECTION------------------------------------------------------------ 

- (void) volumeListener_onOff:(id)sender 
{ 
    UISwitch *onOff = (UISwitch *) sender; 

    if(onOff.on){ 
     volumeListener = [[VolumeListener alloc] init]; 
     [[self.view viewWithTag:54870149] removeFromSuperview]; 
     [self.view addSubview: [volumeListener dummyVolume]]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil]; 
    } 
    else{ 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil]; 
     [[self.view viewWithTag:54870149] removeFromSuperview]; 
    } 
} 


// ADDED THIS SECTION------------------------------------------------------------ 
// ADDED THIS SECTION------------------------------------------------------------ 


- (void)volumeChanged:(NSNotification *)notification{ 
    if(volumeListener.runningVolumeNotification==FALSE){ 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      volumeListener.runningVolumeNotification = TRUE; 
      MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer]; 
      [musicPlayer setVolume:volumeListener.systemVolume]; 

      // do what you want to accomplish here 
      [self.view setAlpha:0.0f]; 
      [UIView beginAnimations:@"flash screen" context:nil]; 
      [UIView setAnimationDuration:0.1f]; 
      [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
      [self.view setAlpha:1.0f]; 
      [UIView commitAnimations]; 

      dispatch_async(dispatch_get_global_queue(0, 0), ^{ 
       volumeListener.runningVolumeNotification = FALSE; 
      }); 
     }); 
    } 
} 

// ADDED THIS SECTION------------------------------------------------------------ 
// ADDED THIS SECTION------------------------------------------------------------ 

#pragma mark - UITextField Delegate method implementation 

-(BOOL)textFieldShouldReturn:(UITextField *)textField{ 
    [self sendMyMessage]; 
    return YES; 
} 


#pragma mark - IBAction method implementation 

- (IBAction)sendMessage:(id)sender { 
    [self sendMyMessage ]; 
} 

- (IBAction)cancelMessage:(id)sender { 
    [self sendMyadditionMessage ]; 
} 


#pragma mark - Private method implementation 

// THIS IS THE SECTION FOR ADDITION ---------------------------------------------------------- 
// --------------------------------------------------------------------------------------------- 
// --------------------------------------------------------------------------------------------- 

-(void)sendMyMessage{ 


    NSData *dataToSend = [_txtMessage.text dataUsingEncoding:NSUTF8StringEncoding]; 
    NSArray *allPeers = _appDelegate.mcManager.session.connectedPeers; 
    NSError *error; 

    [_appDelegate.mcManager.session sendData:dataToSend 
            toPeers:allPeers 
            withMode:MCSessionSendDataReliable 
             error:&error]; 

    if (error) { 
     NSLog(@"%@", [error localizedDescription]); 
    } 

    [_tvChat setText:[_tvChat.text stringByAppendingString:[NSString stringWithFormat:@"\n%@\n\n", _txtMessage.text]]]; 
    [_txtMessage setText:@""]; 

    // THIS IS WHAT STARTS THE COUNT FOR THE TEXT 
    NSString *numValue = [[NSString alloc] initWithFormat:@"%d", Count--]; 
    _txtMessage.text = numValue; 
    // END CODE FOR COUNT 

} 


// START SECTION FOR ADDITION --------------------------------------------------------------- 
// --------------------------------------------------------------------------------------------- 
// --------------------------------------------------------------------------------------------- 

-(void)sendMyadditionMessage{ 
    NSData *dataToSend = [_txtMessage.text dataUsingEncoding:NSUTF8StringEncoding]; 
    NSArray *allPeers = _appDelegate.mcManager.session.connectedPeers; 
    NSError *error; 

    [_appDelegate.mcManager.session sendData:dataToSend 
            toPeers:allPeers 
            withMode:MCSessionSendDataReliable 
             error:&error]; 

    if (error) { 
     NSLog(@"%@", [error localizedDescription]); 
    } 

    [_tvChat setText:[_tvChat.text stringByAppendingString:[NSString stringWithFormat:@"\n%@\n\n", _txtMessage.text]]]; 
    [_txtMessage setText:@""]; 

    // THIS IS WHAT STARTS THE COUNT FOR THE TEXT 
    NSString *numValue = [[NSString alloc] initWithFormat:@"%d", Count++]; 
    _txtMessage.text = numValue; 
    // END CODE FOR COUNT 


    if(Count == 3) { 
     return [self vibrate]; 
    }; 
} 


// END SECTION FOR ADDITION --------------------------------------------------------------- 
// --------------------------------------------------------------------------------------------- 
// --------------------------------------------------------------------------------------------- 

-(void)vibrate 
{ 
    NSLog(@"I'm vibrating"); 
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 
} 


-(void)didReceiveDataWithNotification:(NSNotification *)notification{ 
    MCPeerID *peerID = [[notification userInfo] objectForKey:@"peerID"]; 
    NSString *peerDisplayName = peerID.displayName; 

    NSData *receivedData = [[notification userInfo] objectForKey:@"data"]; 
    NSString *receivedText = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]; 

    [_tvChat performSelectorOnMainThread:@selector(setText:) withObject:[_tvChat.text stringByAppendingString:[NSString stringWithFormat:@"%@ wrote:\n%@\n\n", peerDisplayName, receivedText]] waitUntilDone:NO]; 
} 

@end 
+0

不,你不能這樣做。 – Fogmeister

+4

忽略您之前問題的答案不會改變結果。 http://stackoverflow.com/questions/27786923/physical-volume-control-buttons-assigned-to-ios-app-buttons – Fogmeister

+0

請將您的問題中的代碼縮小到相關代碼(音量按鈕事件和兩種方法你想打電話 – rmaddy

回答

1

我可以提一個建議?使用RBVolumeButtons,這是一個開放源代碼類,可讓您輕鬆竊取音量按鈕。我在我的應用程序中使用它,它完美地工作。如果您對如何使用它有任何疑問,請告訴我。

+0

你可以分享你如何使用它?我無法讓它工作。例如。你在哪裏創建'RBVolumeButtons'對象?在'viewWillAppear'或'viewDidLoad'中? –