帧动画不起作用

时间:2016-03-14 11:02:55

标签: ios objective-c uiview uianimation

viewDidLoad :

topBarMenu = [[TopBarMenu alloc] initWithFrame:CGRectMake(0, 64, 1024, 0)];
    [self.view addSubview:topBarMenu];
    topBarMenu.clipsToBounds = YES;


- (void)menuButton_TouchUpInside:(TopBarIcon *)sender
{
    isTopBarMenuShown = !isTopBarMenuShown;

    if (isTopBarMenuShown) {

        [UIView animateWithDuration:1.5 animations:^{
            topBarMenu.frame = CGRectMake(0, 64, 1024, 600);
                                }];
    }else {

        [UIView animateWithDuration:1.5 animations:^{
            topBarMenu.frame = CGRectMake(0, 64, 1024, 0);
              }];
    }
}

在我的代码中,我希望动画显示和隐藏我的菜单。显示非常阶梯,看起来不好看。隐藏在没有任何动画的情况下立即移除屏幕。如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

试试这个

 if (sideView.frame.origin.x >= 0 )
    {
        [UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewKeyframeAnimationOptionBeginFromCurrentState animations:^{
            sideView.frame=CGRectMake(-400, 45, CGRectGetWidth(sideView.frame), CGRectGetHeight(sideView.frame));
        } completion:^(BOOL finished) {

        }];
    }

    else
    {
        [UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewKeyframeAnimationOptionBeginFromCurrentState animations:^{
            sideView.frame=CGRectMake(0, 45, CGRectGetWidth(sideView.frame), CGRectGetHeight(sideView.frame));
        } completion:^(BOOL finished)
         {

         }];
    }

答案 1 :(得分:0)

使用Autolayout和Animation时,您需要设置常量。因此,在开始动画之前设置常量。因此,创建y位置约束的出口。

topbaryposition.constant=0; (IBOutlet of Top position (Y postion of Topbar))


- (void)menuButton_TouchUpInside:(TopBarIcon *)sender
{
    isTopBarMenuShown = !isTopBarMenuShown;

    if (isTopBarMenuShown) {

        [UIView animateWithDuration:1.5 animations:^{
            topBarMenu.frame = CGRectMake(0, 64, 1024, 600);
                                }];
    }else {

        [UIView animateWithDuration:1.5 animations:^{
            topBarMenu.frame = CGRectMake(0, 64, 1024, 0);
              }];
    }
}

类似的东西,

修改: -

我找到了你的问题。

当您隐藏顶部栏时,您的问题是在其他地方继续使用框架。您只将高度设置为 0 ,但您还需要将 y positioan设置为0

类似的东西,

topBarMenu.frame = CGRectMake(0, 0, 1024, 0);




- (void)menuButton_TouchUpInside:(TopBarIcon *)sender
{
    isTopBarMenuShown = !isTopBarMenuShown;

    if (isTopBarMenuShown) {

        [UIView animateWithDuration:1.5 animations:^{
            topBarMenu.frame = CGRectMake(0, 64, 1024, 600);
                                }];
    }else {

        [UIView animateWithDuration:1.5 animations:^{
            topBarMenu.frame = CGRectMake(0, 0, 1024, 0);
              }];
    }
}

答案 2 :(得分:0)

我认为直接设置框架不是一个很好的方法,我建议更改框架更合适。 如:

- (void)menuButton_TouchUpInside:(TopBarIcon *)sender
{
    isTopBarMenuShown = !isTopBarMenuShown;

    if (isTopBarMenuShown) {

        [UIView animateWithDuration:1.5 animations:^{
            CGRect rect = topBarMenu.frame;
            rect.size.height = 600.0f;
            topBarMenu.frame = rect;
            //topBarMenu.frame = CGRectMake(0, 64, 1024, 600);
                                }];
    } else {

        [UIView animateWithDuration:1.5 animations:^{
            CGRect rect = topBarMenu.frame;
            rect.size.height = 0.0f;
            topBarMenu.frame = rect;
            //topBarMenu.frame = CGRectMake(0, 64, 1024, 0);
              }];
    }
}