0

我有一个 UIView,它离屏 400 像素到左边,宽 280 像素。

我正在尝试将主 UIView 向右移动 280 像素,并使屏幕外 UIView 像侧边栏一样同时在屏幕上向右移动。

我一直被困在这里。

有没有办法在代码中做到这一点并使用 NSLayoutConstraints?

编辑:

-(void) setUpSidebar{
  self.sidebarViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SidebarVC"];
  self.sidebarViewController.view.frame = CGRectMake(-400, 0, 280, self.sidebarViewController.view.bounds.size.height);
  [self.navigationController addChildViewController:self.sidebarViewController];
  [self.navigationController.view addSubview:self.sidebarViewController.view];
  [self.view insertSubview:self.sidebarViewController.view aboveSubview:self.view];
}

-(void)slideToTheRight{
  [UIView animateWithDuration:0.5 animations:^{
    self.sidebarOpened = YES;
    //self.sidebarViewController.view.frame = CGRectMake(-280, 0, 280, self.sidebarViewController.view.bounds.size.height);
    self.view.frame = CGRectMake(280, 0, self.view.bounds.size.width, self.view.bounds.size.height);



  }];

  [UIView animateWithDuration:0.1 animations:^{
    self.sidebarViewController.view.transform = CGAffineTransformMakeTranslation(120, 0);
  }];
}

-(void) slideBack{
  [UIView animateWithDuration:0.3 animations:^{
    self.sidebarOpened = NO;
    //self.sidebarViewController.view.frame = CGRectMake(-280, 0, 280, self.sidebarViewController.view.bounds.size.height);
    self.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
  }];

  [UIView animateWithDuration:0.2 animations:^{
    self.sidebarViewController.view.transform = CGAffineTransformMakeTranslation(-120, 0);
  }];
}
4

1 回答 1

0

当然,你可以做到这一点。为要更改的约束创建一个 IBOutlet,并在代码中修改其常量参数。所以,如果你有一个出口到名为 leftCon 的超级视图的左边缘的约束,你可以这样做:

self.leftCon.constant += 200;
于 2013-08-16T05:19:30.503 回答