将 UIView 添加到 KeyWindow,调用 pushViewController 时如何对屏幕外的 UIView 进行动画处理?

Add UIView to KeyWindow, How to Animate UIView Off Screen When pushViewController is Called?

提问人:Chris 提问时间:2/27/2016 更新时间:2/27/2016 访问量:469

问:

我已经在我的中添加了一个,它工作正常。UIView[UIApplication sharedApplication].keyWindow

当点击上的按钮时,我想将新的按钮推到底层UIViewUIViewControllerNavigationController

这一切都有效,但是我怎样才能使动画离开屏幕,向左,与底层?UIViewUIViewController

iOS Objective-C 动画 UIVieinController KeyWindow

评论

0赞 beyowulf 2/27/2016
如果希望它使用视图控制器进行动画处理,为什么不将其添加为该视图控制器视图的子视图呢?
0赞 Chris 2/27/2016
之所以添加到 中,是因为设计者希望它出现在导航栏的顶部(占据整个屏幕)UIViewKeyWindow
0赞 alfreedom 2/27/2016
您是否在 appDelegate 中打开视图?
0赞 beyowulf 2/27/2016
我不知道“(占据整个屏幕)”是什么意思?他们想要在导航栏上方的哪个位置?添加屏幕截图?

答:

0赞 Achu22 2/27/2016 #1

您可以对自定义视图进行动画处理,同时将控件传递给邮件视图控制器,以将下一个视图控制器推送到导航控制器的堆栈上...下面是我模拟的示例代码。自定义视图的动画应与视图控制器动画同步。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _v = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(0, 0, 50, 30)];
    [btn setTitle:@"Button" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_v addSubview:btn];
    _v.backgroundColor = [UIColor redColor];
    [[[UIApplication sharedApplication] keyWindow] addSubview:_v];

}

- (void)btnClicked:(id)sender {

    NSLog(@"Btn Clicked");

    [UIView animateWithDuration:0.2
                          delay:0.1
                        options: UIViewAnimationOptionCurveEaseOut
                     animations:^
     {
         _v.frame = CGRectMake(-_v.frame.size.width, 

_v.frame.origin.y, _v.frame.size.width, _v.frame.size.height);
         }
                         completion:^(BOOL finished)
         {
         }];
    UIViewController *c = [self.storyboard instantiateViewControllerWithIdentifier:@"TestVC"];
    [self.navigationController pushViewController:c animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

评论

0赞 alfreedom 2/27/2016
我认为您还必须执行[[[UIApplication sharedApplication] keyWindow] bringSubviewToFront:_v];添加子视图后