2014-12-12 3 views
0

두 개의 사이드 메뉴를 구현하려고합니다. 스크린 샷에서 볼 수 있듯이 두 개의 단추와 메뉴가 추가되었습니다. 그러나 어떻게 클릭하면 어떤 버튼을 정의 할 수 있습니까? 내 코드는 왼쪽면에서 작동하지만 오른쪽면에서는 작동하지 않습니다.iOS 사이드 메뉴 버튼

어떤 버튼을 클릭했는지 알면 나머지 부분을 수정할 수있었습니다.

- (void)viewDidLoad { 


    sideBarRight = [[CDSideBarController alloc] initWithImages:imageList]; 
    sideBarRight.delegate = self; 

    sideBarLeft = [[CDSideBarController alloc] initWithImages:imageList]; 
    sideBarLeft.delegate = self; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    // Right Menu Button 
    [sideBarRight insertMenuButtonOnView:[UIApplication sharedApplication].delegate.window atPosition:CGPointMake(self.view.frame.size.width - 70, 50) atSite:@"Right"]; 
    // Left Menu Button 
    [sideBarLeft insertMenuButtonOnView:[UIApplication sharedApplication].delegate.window atPosition:CGPointMake(self.view.frame.size.width-980, 50) atSite:@"Left"]; 
} 

//CDSideBarController 

- (CDSideBarController*)initWithImages:(NSArray*)images 
{ 
    _menuButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    _menuButton.frame = CGRectMake(0, 0, 40, 40); 
    [_menuButton setImage:[UIImage imageNamed:@"menuIcon.png"] forState:UIControlStateNormal]; 
    [_menuButton addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchUpInside]; 

    _backgroundMenuView = [[UIView alloc] init]; 
    _menuColor = [UIColor whiteColor]; 
    _buttonList = [[NSMutableArray alloc] initWithCapacity:images.count]; 

    return self; 
} 

- (void)insertMenuButtonOnView:(UIView*)view atPosition:(CGPoint)position atSite:(NSString*)direction 
{ 
    _menuButton.frame = CGRectMake(position.x, position.y, _menuButton.frame.size.width, _menuButton.frame.size.height); 
    [view addSubview:_menuButton]; 

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissMenu)]; 
    [view addGestureRecognizer:singleTap]; 

    for (UIButton *button in _buttonList) 
    { 
     [_backgroundMenuView addSubview:button]; 
    } 

    if([direction isEqualToString:@"Right"]) 
    { 
    _backgroundMenuView.frame = CGRectMake(view.frame.size.width, 0, 90, view.frame.size.height); 
    } 
    if([direction isEqualToString:@"Left"]) 
    { 
    _backgroundMenuView.frame = CGRectMake(-90, 0, 90, view.frame.size.height); 
    } 
    _backgroundMenuView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5f]; 
    [view addSubview:_backgroundMenuView]; 
} 

// here is the thing needs to be fixed 

- (void)performDismissAnimation 
{ 
    [UIView animateWithDuration:0.4 animations:^{ 
     _menuButton.alpha = 1.0f; 
     _menuButton.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0); 
     _backgroundMenuView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, -90, 0); 
    }]; 
} 

- (void)performOpenAnimation 
{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [UIView animateWithDuration:0.4 animations:^{ 
      _menuButton.alpha = 0.0f; 
      _menuButton.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0); 
      _backgroundMenuView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, +90, 0); 
     }]; 
    }); 

enter image description here enter image description here

답변

0

좋아, I는 다음과 같은 문제를 해결하는 방법을 발견 한 I 다른 매개 변수를 추가 -tagID- I는 각 버튼을 초기화 IF-다른 조건 afterwords

추가 때
- (CDSideBarController*)initWithImages:(NSArray*)images atTag:(int)tagID 
{ 
    _menuButton.tag=tagID; 

} 

- (void)performDismissAnimation 
{ 
    [UIView animateWithDuration:0.4 animations:^{ 
     _menuButton.alpha = 1.0f; 
     _menuButton.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0); 
     if(_menuButton.tag==2) 
     { 
     _backgroundMenuView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, -90, 0); 
     } 
     if(_menuButton.tag==1) 
     { 
      _backgroundMenuView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0); 
     } 
    }]; 
} 

- (void)performOpenAnimation 
{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [UIView animateWithDuration:0.4 animations:^{ 
      _menuButton.alpha = 0.0f; 
      if(_menuButton.tag==2) 
      { 
      _menuButton.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0); 
      _backgroundMenuView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, +90, 0); 
      } 
      if(_menuButton.tag==1) 
      { 
       _menuButton.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, -90, 0); 
       _backgroundMenuView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, -90, 0); 
      } 
     }]; 
    });