iOS 从另一个视图的一侧滑动 UIView(菜单)

问题描述 投票:0回答:2

我想在按钮单击事件后从视图的一侧滑入菜单。再次单击按钮后,菜单应该从大视图中滑出...

我尝试了 CATransition 但无法解决这个问题...

CATransition *animation = [CATransition animation];
[animation setDuration:1];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromBottom];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]] ;

[[menuView layer] addAnimation:animation forKey:@"@asdf"];        
[menuView setCenter:CGPointMake([menuView center].x, [menuView center].y + 450)];

它有点工作,但我不明白为什么..:-/

ios cocoa-touch uiview ios4 core-animation
2个回答
0
投票

如果您想自己实现它,可以采用的一种方法是创建自定义视图并为视图控制器中的按钮添加按钮单击事件。

还创建一个布尔变量来标识您的自定义视图是否可见。

单击时,如果菜单视图不可见,则执行以下操作:

-(void)viewDidLoad
{
    ...

    // ---------------------------------------------
    // create the custom menu view off screen
    // ---------------------------------------------
    menuView = [[MenuView alloc] initWithFrame:CGRectMake(-320, 0, 320, 480)];
    menuView.alpha = 0; // hide it so it doesn't show at the start, only show when slide in

    [self.view addSubview:menuView];

    ...
}

-(void)toggleMenu
{
    if(menuIsVisible)
    {
        menuIsVisible = NO;

        [self hideMenu];
    }
    else
    {
        menuIsVisible = YES;

        [self showMenu];
    }
}

-(void)hideMenu
{
    [UIView animateWithDuration:0.5 animations:^{
        // note CGAffineTransforms doesn't use coordinates, they use offset
        // so an offset of (0,0) would bring the menuView back to the coordinate
        // when it was first instantiated, in our case, (-320, 0).
        menuView.transform = CGAffineTransformMakeTranslation(0,0);
    } completion:^{
        // hide the menu
        menuView.alpha = 0;
    }];
}

-(void)showMenu
{
    // show the menu
    menuView.alpha = 1;

    [UIView animateWithDuration:0.5 animations:^{
        // note CGAffineTransforms doesn't use coordinates, they use offset
        // so an offset of (320,0) from coordinate (-320,0) would slide the
        // menuView out into view
        menuView.transform = CGAffineTransformMakeTranslation(320,0);
    }];
}

0
投票

ViewDeck这样的控制器将使您能够做到这一点。

它是开源代码,将添加一个由按钮控制的菜单,该按钮在单击时滑入,再次单击时滑出,类似于 Facebook 和 Path 的菜单。您还可以通过幻灯片获得触摸事件。

GitHub 页面上有一个如何使用它的示例:https://github.com/Inferis/ViewDeck#how-to-use-it 以及源代码中提供的几个示例项目。

© www.soinside.com 2019 - 2024. All rights reserved.