0
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);
              }];
    }
}

In my code i want to animate showing and hiding my menu. Showing is very stepped and dosen't look nice. Hiding immediately remove the screen without any animation. How to solve this problem ?

hds
  • 33
  • 1
  • 8

3 Answers3

0

try this

 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)
         {

         }];
    }
0

You need to set constant when you are using Autolayout and Animation. So, set constant before you begin the Animation. So, create outlet of constrain of y position.

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);
              }];
    }
}

Something like ,

Edit :-

I found your Issue .

Your issue is seeting the frame in else when you hiding the topbar.you have set only height as 0 but you also need to set y positioan as 0..

Something like ,

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);
              }];
    }
}
Badal Shah
  • 7,541
  • 2
  • 30
  • 65
  • Thanks but it didn't solve my problem. Ehh. Found solution , i added [self.view layoutIfNeeded] and works fine. – hds Mar 14 '16 at 11:44
0

I think set frame directly is not a very good method,I suggest to change frame is more appropriate. Such as:

- (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);
              }];
    }
}
MarkoHiel
  • 15,481
  • 2
  • 21
  • 29
deney
  • 1
  • 1