从其他视图控制器调用时,动画未运行

时间:2013-06-13 12:33:41

标签: ios

我的ipad应用程序中有一个侧边栏,当有这个方法toggleMenu正在执行动画但是当我从另一个视图控制器调用该方法时,它不执行动画。该方法运行正常,但没有动画

方法是

-(IBAction)toggleMenu
{

[UIView beginAnimations:@"Menu Slide" context:nil];
[UIView setAnimationDuration:0.2];

if(self.contentView.frame.origin.x == 0) //Menu is hidden
{

    NSLog(@"show menu");

    CGRect newFrame = CGRectOffset(self.contentView.frame, self.menuView.frame.size.width, 0.0);
    self.contentView.frame = newFrame;

}
else //Menu is shown
{

    NSLog(@"hide menu");

    [menuTableView reloadData];
    CGRect newFrame = CGRectOffset(self.contentView.frame, -(self.menuView.frame.size.width), 0.0);
    self.contentView.frame = newFrame;

}

[UIView commitAnimations];

}

1 个答案:

答案 0 :(得分:1)

试试这个,当你想要动画时,你必须从另一个视图控制器发送通知..

  [[NSNotificationCenter defaultCenter] postNotificationName:@"animationstart"        object:nil];
slidemenuviewcontroller中的

  -(void)viewDidLoad

{
 [[NSNotificationCenter defaultCenter] removeObserver:self];
 [[NSNotificationCenter defaultCenter] addObserver:self    selector:@selector(toggleMenu) name:@"animationstart" object:nil];

}

- (IBAction)toggleMenu
{
  [UIView beginAnimations:@"Menu Slide" context:nil];
  [UIView setAnimationDuration:0.2];

if(self.contentView.frame.origin.x == 0) //Menu is hidden
{

NSLog(@"show menu");

CGRect newFrame = CGRectOffset(self.contentView.frame, self.menuView.frame.size.width, 0.0);
self.contentView.frame = newFrame;

 }
 else //Menu is shown
 {

NSLog(@"hide menu");

[menuTableView reloadData];
CGRect newFrame = CGRectOffset(self.contentView.frame, -   (self.menuView.frame.size.width), 0.0);
self.contentView.frame = newFrame;

}

[UIView commitAnimations];


}