【问题标题】:Update view of a NavigationController simulating a push模拟推送的 NavigationController 的更新视图
【发布时间】:2012-02-28 02:07:11
【问题描述】:

我可以用动画推送视图并且只更新它的内容吗? 我有 4 个相同的视图,所以我不想创建 4 个 xib,而是想更新视图,但给用户一种他已经切换视图的印象。

所以我真的不想推动,我只想给用户留下印象。 这可能吗?

【问题讨论】:

  • 那么你已经尝试过了。你能在没有动画的情况下切换视图吗?
  • 仍然没有尝试在没有动画的情况下切换视图。那应该不是问题。我只需要调用我的控制器 .text 并提供新内容。但是如果没有动画,用户就无法感知到他改变了视图。

标签: iphone ios view uinavigationcontroller xib


【解决方案1】:

您可以为左侧的一个视图设置动画,同时从右侧为下一个视图设置动画,这与推送相同(假设下一个和当前视图已经是子视图)

UIView *nextView.frame = CGRectOffset(self.view.frame, self.view.frame.size.width, 0.0f);

[UIView animateWithDuration:0.3f animations:^{
    currentView.frame = CGRectOffset(currentView.frame, -self.view.frame.size.width, 0.0f);
    nextView.frame = CGRectOffset(nextView.frame, -self.view.frame.size.width, 0.0f);
}];

如果您也尝试更改视图的 alpha 属性,它可能看起来会更好

【讨论】:

  • currentView 将等于 self.view 而 nextView 将是我使用 UIViewController 创建的?
  • 在那个例子中,它们都是 self.view 的子视图。如果您正在使用视图控制器创建视图,然后进行虚假推送,这似乎毫无意义,您应该只推送视图控制器的新实例
【解决方案2】:

我建立了一个简单的项目,其中有一个 VC 和中间的按钮来触发动画。这是绑定到按钮的代码:

- (IBAction)pushNewView:(id)sender {
[UIView animateWithDuration:0.5 animations:^{
    // this moves the view off screen to the left
    self.view.frame = CGRectMake(-320, 0, 320, 480);
} completion:^(BOOL finished) {
    // this pops the view over to the right side of the screen
    self.view.frame = CGRectMake(320, 0, 320, 480);
    [UIView animateWithDuration:0.5 animations:^{
        // and this slides it in from the right
        self.view.frame = CGRectMake(0, 0, 320, 480);
    }];
}]; }

您可以调整时间以使其看起来更“原生”,并使用前面提到的 alpha 版本。您可以在两个动画块中的任何一个中更改它。

【讨论】:

  • 作为一点额外说明,确保将 self.window 的背景颜色更改为与动画视图的背景颜色相同,否则屏幕所在的位置会出现短暂的一秒黑色(窗口的默认背景颜色)。将 alpha 设置为 0.3,然后再设置回 1.0 也很有效。
【解决方案3】:

最好的方法(恕我直言)是在没有动画的情况下弹出当前视图,然后立即再次推送相同的视图。

myNavigationController = self.navigationController;
[myNavigationController popViewControllerAnimated:NO];
[myNavigationController pushViewController:myViewController animated:YES];

这是我用来提供无限数量视图效果的技术的一部分,而实际上只使用/保留一个在内存中。

当想要处理“后退”导航时,您可以推送一个不带动画的新导航,然后用动画弹出它。

myNavigationController = self.navigationController;
[myNavigationController pushViewController:myViewController animated:NO];
[myNavigationController popViewControllerAnimated:YES];

【讨论】:

  • 您能分享一下您是如何访问 myNavigationController 和 myNavController 的吗?
  • 啊,myNavController 和 myNavigationController 是一样的(复制和粘贴错误,我现在修复)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多