我正在尝试实现两个侧面菜单。如截图所示,我添加了两个按钮和菜单。但是,我怎么能够定义单击哪个按钮?我的代码适用于左侧,但不适用于右侧。
如果我知道点击了哪个按钮,那么我就可以修改其余细节。
- (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);
}];
});
答案 0 :(得分:0)
好的,我已经找到了解决问题的方法如下,我在初始化每个按钮时添加了另一个参数-tagID-并在ifwords后添加了if-else条件
- (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);
}
}];
});