2012-03-28 52 views
-1

我有一個UIbutton,我想作爲操縱桿工作。所以我試圖在同一個按鈕上添加一些手勢識別器。 我有這個在我的代碼現在:如何在UIButton上使用UISwipeGestureRecognizer?

@implementation CUETutorialSixteenClusterRootController 

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

- (id)init { 
    self = [super init]; 
    if (self) { 
     _leftInActiveView = [[NSMutableArray alloc] init]; 
     _rightInActiveViews = [[NSMutableArray alloc] init]; 
     _centerInActiveViews = [[NSMutableArray alloc]init]; 
     _centerActiveViews = [[NSMutableArray alloc]init]; 

     UIImageView * inActLeftView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 98)] autorelease]; 
     inActLeftView.image = [UIImage imageNamed:@"1-am-station_01.png"]; 
     [_leftInActiveView addObject:inActLeftView]; 


     UIImageView * inActRightView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 98)] autorelease]; 
     inActRightView.image = [UIImage imageNamed:@"1-am-station_03.png"]; 
     [_rightInActiveViews addObject:inActRightView]; 

     NSArray * imagesName = [NSArray arrayWithObjects:[UIImage imageNamed:@"1_start.png"], 
           [UIImage imageNamed:@"2_apptray_audio_selected.png"], 
           [UIImage imageNamed:@"2_apptray_phone_selected.png"], 
           [UIImage imageNamed:@"4_phonemenu_contacts_highlighted.png"], 
           [UIImage imageNamed:@"5_phonemenu_recentcalls_highlighted.png"], 
           [UIImage imageNamed:@"6_recent_calls.png"], 
           [UIImage imageNamed:@"7_bottom_hit.png"], 
           [UIImage imageNamed:@"8_recent_call_details.png"], 
           [UIImage imageNamed:@"9_calling.png"], 
           [UIImage imageNamed:@"9_calling_myphone.png"], 
           [UIImage imageNamed:@"9_call_ended.png"],nil]; 
     NSLog(@"Count:%d",[imagesName count]); 
     for (int i =0; i<11; i++) { 
      UIImageView * inActCenterView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 226, 98)] autorelease]; 
      inActCenterView.image = [imagesName objectAtIndex:i]; 
      [_centerActiveViews addObject:inActCenterView]; 
      [_centerInActiveViews addObject:inActCenterView]; 
     } 
    } 
    return self; 
} 

-(void)showClusterType:(clusterType)cluster{ 
    switch (cluster) { 
      //Base Cluster is analog 
     case kClusterTypeBase: 
     { 
      [self.fullClusterContainerView removeFromSuperview]; 
      [self.view addSubview:self.baseClusterContainerView]; 
      [self.view addSubview:self.baseClusterImageView]; 

      CUETutorialSixteenAplicationHomeScreen * clusAppScreen = [[CUETutorialSixteenAplicationHomeScreen alloc] initWithDelegate:self]; 
      clusAppScreen.dataSource = self; 
      clusAppScreen.rootController = self; 

      self.clusterHomeScreen = clusAppScreen; 

      [self.baseClusterContainerView addSubview:clusAppScreen.view]; 
      [clusAppScreen release]; 

      /* 
      [self.view bringSubviewToFront:self.steeringWheelImageView]; 
      [self.view bringSubviewToFront:self.promptLabel]; 
      [self.view bringSubviewToFront:[self.view viewWithTag:kFavDownButton]]; 
      [self.view bringSubviewToFront:[self.view viewWithTag:kFavUpButton]]; 
      [self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonUp]]; 
      [self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonLeft]]; 
      [self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonRight]]; 
      [self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonDown]]; 
      [self.view bringSubviewToFront:[self.view viewWithTag:kRightButtonCenter]]; 
      */ 

      [self.view bringSubviewToFront:self.promptLabel]; 
     } 
      break; 
      //Full cluster is digital 
     case kClusterTypeFull: 
     { 
      [self.baseClusterImageView removeFromSuperview]; 
      [self.baseClusterContainerView removeFromSuperview]; 
      [self.view addSubview:self.fullClusterContainerView]; 

     } 
      break; 
     default: 
      break; 
    } 
} 


- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView 
{ 
} 
*/ 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    _step =1; 
    UISwipeGestureRecognizer *recognizer; 

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)]; 
    [self.gestureRecieverButton addGestureRecognizer:recognizer]; 
    [recognizer release]; 
    recognizer = nil; 

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)]; 
    [self.gestureRecieverButton addGestureRecognizer:recognizer]; 
    [recognizer release]; 
    recognizer = nil; 

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; 
    [self.gestureRecieverButton addGestureRecognizer:recognizer]; 
    [recognizer release]; 
    recognizer = nil; 

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; 
    [self.gestureRecieverButton addGestureRecognizer:recognizer]; 
    [recognizer release]; 
    recognizer = nil; 


} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    time=0; 

    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 
-(void)dealloc{ 
    [super dealloc]; 
    [_leftInActiveView release]; 
    [_rightInActiveViews release]; 
    [_centerActiveViews release]; 
    [_centerInActiveViews release]; 
} 

-(void)animateToView:(UIView *)aview{ 
    if(aview){ 
     //CGRect rect = [aview convertRect:aview.bounds toView:self.view]; 
     CGRect rect = aview.frame; 
     [self animateHandFromRect:self.promptLabel.frame toRect:rect]; 
    } 
    else{ 
     [self animateHandToNSValueRect:nil]; 
    } 
} 

-(void)finishTutorial{ 
    [self displayPromptText:@"TUTORIAL COMPLETE"]; 
    [self displayNavigationBarWithTitle:@"TUTORIAL COMPLETE"]; 

    [self performBlock:^{ 
     [self.clusterHomeScreen.view removeFromSuperview]; 
     [self showScoresScreen]; 
    }afterDelay:1]; 
} 
-(void)animateHandToCenterButton 
{ 
    [self displayPromptText:@"TAP RIGHT SIDE CENTER BUTTON"]; 
    [self displayNavigationBarWithTitle:@"TAP RIGHT SIDE CENTER BUTTON"]; 
    [self animateToView:self.rCenterButton]; 
} 
-(void)animateHandToDownButton { 
    [self displayPromptText:@"TAP RIGHT SIDE DOWN BUTTON"]; 
    [self displayNavigationBarWithTitle:@"TAP RIGHT SIDE DOWN BUTTON"]; 
    [self animateToView:self.rDownButton]; 

} 

-(void)animateHandToUpButton{ 
    [self displayPromptText:@"TAP RIGHT SIDE UP BUTTON"]; 
    [self displayNavigationBarWithTitle:@"TAP RIGHT SIDE UP BUTTON"]; 
    [self animateToView:self.rUpButton]; 

} 

-(void)replaceCallScreen{ 
    [self performBlock:^{ 
     [self pressedRightButtonCenter]; 
    } afterDelay:2.0]; 

} 

-(void)changeTimer { 
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(startTicking) userInfo:nil repeats:YES]; 

} 

-(void)startTicking { 
    time = time +1; 
    self.clusterHomeScreen.callTimer.text = [NSString stringWithFormat:@"00:0%d",time]; 
    if (time==9) { 
     [timer invalidate]; 
    } 
} 
-(void) animateHandToVolUpButton { 
    [self displayPromptText:@"TAP VOLUME UP BUTTON"]; 
    [self displayNavigationBarWithTitle:@"TAP VOLUME UP BUTTON"]; 
    [self animateToView:self.rVolUpButton]; 

} 

-(void) animateHandToVolDownButton { 
    [self displayPromptText:@"TAP VOLUME DOWN BUTTON"]; 
    [self displayNavigationBarWithTitle:@"TAP VOLUME DOWN BUTTON"]; 
    [self animateToView:self.rVolDownButton]; 
} 

-(void)removeVolumeView { 
    [self pressedRightButtonCenter]; 
    [self.clusterHomeScreen runNextStep]; 
} 

-(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer { 
    NSLog(@"Swipe received.%@",recognizer); 
} 

-(IBAction)buttonClicked:(id)sender 
{  
    if ([sender tag]==kRightButtonDown && _step ==1) 
    { 
     [self pressedRightButtonDown]; 
     [self displayPromptText:@"TAP AGAIN"]; 
     [self displayNavigationBarWithTitle:@"TAP AGAIN"]; 
     _step++; 
    } 
    else if ([sender tag]==kRightButtonDown && _step == 2) 
    { 
     [self pressedRightButtonDown]; 
     [self animateToView:self.rCenterButton]; 
     [self displayPromptText:@"TAP CENTER BUTTON"]; 
     [self displayNavigationBarWithTitle:@"TAP CENTER BUTTON"]; 
     _step++; 
    } 
    else if ([sender tag]==kRightButtonCenter && _step == 3) 
    { 
     [self pressedRightButtonCenter]; 
     [self animateToView:self.rUpButton]; 
     _step++; 
    } 
    else if ([sender tag]==kRightButtonUp && _step == 4) 
    { 
     [self pressedRightButtonUp]; 
     [self displayPromptText:@"TAP CENTER BUTTON"]; 
     [self displayNavigationBarWithTitle:@"TAP CENTER BUTTON"]; 
     [self animateToView:self.rCenterButton]; 

     _step++; 
    } 

    else if ([sender tag]==kRightButtonCenter && _step==5){ 
     [self pressedRightButtonCenter]; 
     [self displayPromptText:@"TAP DOWN BUTTON"]; 
     [self displayNavigationBarWithTitle:@"TAP DOWN BUTTON"]; 
     [self animateToView:self.rDownButton]; 
     _step++; 
    } 
    else if([sender tag]==kRightButtonDown && _step==6){ 
     [self pressedRightButtonDown]; 
     [self animateToView:self.rCenterButton]; 
     [self displayPromptText:@"TAP CENTER BUTTON"]; 
     [self displayNavigationBarWithTitle:@"TAP CENTER BUTTON"]; 
     _step++; 
    } 
    else if ([sender tag]==kRightButtonCenter && _step==7){ 
     [self pressedRightButtonCenter]; 
     _step++; 
    } 
    else if ([sender tag]==kRightButtonCenter && _step==8) 
    { 
     [self pressedRightButtonCenter]; 
     self.clusterHomeScreen.callTimer.hidden = NO; 
     [self.clusterHomeScreen runNextStep]; 
     _step++; 
    } 
    else if ([sender tag]==kRightButtonCenter && _step==9) 
    { 
     self.clusterHomeScreen.callTimer.hidden = YES; 
     [self pressedRightButtonCenter]; 

     _step++; 
    } 
    else if ([sender tag]==kRightButtonCenter && _step==10) 
    { 
     [self pressedRightButtonCenter]; 
     [self finishTutorial]; 
    } 
    else { 
     [self displayVisualFeedback:kVisualFeedBackIncorrectAnswer]; 
    } 
} 


#pragma CUEClusterHomeSreenDataSource Methods 
-(NSInteger)numberOfViewsForLeftScreen { 

    return [_leftActiveViews count]; 
} 
-(NSInteger)numberOfViewsForRightScreen{ 
    return [_rightActiveViews count]; 
} 
-(NSInteger)numberOfViewsForCenterScreen { 
    return [_centerActiveViews count]; 
} 



-(UIImageView *)inActiveViewForLeftScreenAtIndex:(NSInteger)index{ 
    //if((index < [_leftInActiveView count]) && (index >=0)){ 
    return [_leftInActiveView objectAtIndex:index]; 
    //} 
    //return nil; 
} 
-(UIImageView *)activeViewForLeftScreenAtIndex:(NSInteger)index{ 
    // if((index < [_leftActiveViews count]) && (index >=0)){ 
    return [_leftActiveViews objectAtIndex:index]; 
    //} 
    //return nil; 
} 
-(UIImageView *)inActiveViewForRightScreenAtIndex:(NSInteger)index{ 
    //if((index < [_rightInActiveViews count]) && (index >=0)){ 
    return [_rightInActiveViews objectAtIndex:index]; 
    //} 
    //return nil; 
} 
-(UIImageView *)activeViewForRightScreenAtIndex:(NSInteger)index{ 
    //if((index < [_rightActiveViews count]) && (index >=0)){ 
    return [_rightActiveViews objectAtIndex:index]; 
    //} 
    //return nil; 
} 

-(UIImageView *)inActiveViewForCenterScreenAtIndex:(NSInteger)index{ 
    //if((index < [_leftInActiveView count]) && (index >=0)){ 
    return [_centerInActiveViews objectAtIndex:index]; 
    //} 
    //return nil; 
} 
-(UIImageView *)activeViewForCenterScreenAtIndex:(NSInteger)index{ 
    // if((index < [_leftActiveViews count]) && (index >=0)){ 
    return [_centerActiveViews objectAtIndex:index]; 
    //} 
    //return nil; 
} 

這是我現在得到的錯誤: - [CUETutorialSixteenClusterRootController handleSwipeFrom:]:無法識別的選擇發送到實例0x79b71b0 2012-03-28 13:25:55.724 CUETrainer [1788: 11f03] *終止應用程序由於未捕獲的異常「NSInvalidArgumentException」,原因是:「 - [CUETutorialSixteenClusterRootController handleSwipeFrom:]:無法識別的選擇發送到實例0x79b71b0」 ​​

但它實際上沒有做任何事情。 請幫忙!!!!

+0

見編輯。但仍然沒有工作。 – Ashutosh 2012-03-28 17:23:42

+1

是在.h中聲明的handleSwipeFrom嗎? – 2012-03-28 17:34:56

+0

是的。現在發生的事情是,這是從頂部到底部刷卡工作,但崩潰的一切。 – Ashutosh 2012-03-28 17:38:09

回答

1

錯誤說unrecognized selector sent to instance 0x79b71b0'最有可能在h和.M名稱不同..

1

手勢識別器的目標需要是實現選擇器的對象。現在發生什麼事時,刷卡情況是,手勢識別是做[self.gestureReceiverButton handleSwipeFrom:recognizer]

你可能想這樣的:

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 
+0

我改變self.gestureRecognizer自我,但它仍然不工作 – Ashutosh 2012-03-28 17:12:47

0

變化的每一個實例:

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

問題解決了。要非常小心你的@selectors,你放在這個宏中的東西不會被編譯器驗證。完成代碼將幫助你。

此外,您還將四個手勢識別器添加到一個視圖,而您可以使用一個視圖完成相同的任務。您可以結合使用位或操作的有效方向,就像這樣:

[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown)];

相關問題