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

时间:2011-07-09 05:18:25

标签: ios cocoa-touch uiview ios4 core-animation

我想在按钮点击事件后从视图中的一侧滑入菜单。在另一个Buttonclick之后,菜单应该从大视图中滑出......

我尝试过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)];

它工作了一下,但我不明白为什么..: - /

2 个答案:

答案 0 :(得分:0)

ViewDeck这样的控制器可以让你这样做。

修改: 我不知道还有什么可以添加到这个,主持人。这是一个开源代码,它将添加一个由按钮控制的菜单,该按钮在点击时会滑入,再次点击时会滑出,类似于Facebook和Path所拥有的。你也可以通过幻灯片获得触摸事件。

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

答案 1 :(得分: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);
    }];
}