打开/关闭菜单条件



我正在创建一个小的滑出菜单,而不使用任何库。通过使用UIDynamics,我可以通过单击IBAction按钮滑动打开菜单。

我想问我如何通过点击同一个btn来关闭菜单。菜单可以通过点击另一个btn关闭,例如菜单幻灯片中的"关闭"btn,但我也想通过点击打开它的同一菜单btn关闭它。

这是显示菜单的功能

-(BOOL)showMenu:(BOOL)yesNO{
    //cleaning the screen of any behaviors
    [self.animator removeAllBehaviors];
    //animation will go from side to side
    CGFloat graxityDirectionX = (yesNO) ? 0.1 : -1.0;
    //detecting the collision of the menu
    CGFloat collision = (yesNO) ? menuWidth : -menuWidth;
    //insitating the gravity and telling it work on the menu
    UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.menuView]];
    gravity.gravityDirection = CGVectorMake(graxityDirectionX, 0);
    //adding the animation
    [self.animator addBehavior:gravity];
    //instaniating the collision and telling the menu were to stop
    UICollisionBehavior *collisionMenu = [[UICollisionBehavior alloc] initWithItems:@[self.menuView]];
    //telling the menu where and what to collide it by using an invisible line
    [collisionMenu addBoundaryWithIdentifier:@"menuBoundry" fromPoint:CGPointMake(collision, 580) toPoint:CGPointMake(collision, 0)];
    //adding the collision
    [self.animator addBehavior:collisionMenu];
    //setting the bounce
    UIDynamicItemBehavior *menuBounce = [[UIDynamicItemBehavior alloc]initWithItems:@[self.menuView]];
    menuBounce.elasticity = 0;
    [self.animator addBehavior:menuBounce];
   return YES;
}

这就是我在IBAction 中的称呼

- (IBAction)menuAction:(id)sender {
   [self showMenu:YES];
}

我尝试过不同类型的if语句,但无法将其关闭。

请帮忙。

提前感谢

我误解了动态行为。好的,我建议使用BOOL。声明BOOL

BOOL isMenuVisible;

然后在IBAction 中使用

- (IBAction)menuAction:(id)sender {
    if(isMenuVisible == NO){
       self.isMenuVisible = YES;
       [self showMenu:self.isMenuVisible];
    }else{
       self.isMenuVisible = NO;
       [self showMenu:self.isMenuVisible];
    }
}

此任务永远不要使用布尔值。由于您在方法"showMenu"中有菜单宽度。获取一个类级别的属性,将菜单宽度分配给它。在方法"menuAction"中应用检查。如果菜单宽度大于0,则意味着您必须关闭它。如果菜单宽度小于零,则意味您必须显示它。

最新更新