在UIView动画完成块中使用[self.view removeFromSuperview]会立即删除视图

时间:2014-08-23 00:30:45

标签: ios objective-c uiviewcontroller uiviewanimation childviewcontroller

固定

事实证明动画被调用了两次。我现在已经修复它,所以动画只调用一次,它完美无缺。


我怀疑它可能与我使用子视图控制器有关,但当我尝试用动画删除子视图控制器的视图时,我注意到一些奇怪的行为。

我将子视图控制器与子视图控制器的视图一起添加到父视图,并从屏幕底部为该视图设置动画。这很完美 - 我的问题是当我尝试删除子视图/视图控制器时。我将孩子的视图设置为屏幕底部的动画,然后在完成块中调用[self.view removeFromSuperview],但视图立即被删除(因此不会发生动画)。如果我删除[self.view removeFromSuperview]行,则动画正常工作,但视图不会从父视图控制器的视图中删除。


添加子视图控制器

(这可以按预期工作)

ChildViewController *myChildViewController = [[ChildViewController alloc] init];
myChildViewController.view.frame = CGRectMake(0.f, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - 64.f);

[self addChildViewController:myChildViewController];
[self.view addSubview:myChildViewController.view];
[myChildViewController didMoveToParentViewController:self];

[UIView animateWithDuration:0.75f delay:0.f usingSpringWithDamping:0.6f initialSpringVelocity:0.75f options:0 animations:^{
    myChildViewController.view.frame = CGRectMake(0.f, 64.f, self.view.frame.size.width, self.view.frame.size.height - 64.f);
} completion:nil];


删除子视图控制器

(这不起作用 - 视图会立即删除,而不是在完成后删除)

[UIView animateWithDuration:0.75f delay:0.f usingSpringWithDamping:0.6f initialSpringVelocity:0.75f options:0 animations:^{
    self.view.frame = CGRectMake(0.f, self.view.superview.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
} completion:^(BOOL finished) {
    if (finished)
    {
        [self willMoveToParentViewController:self.parentViewController];
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }
}];



我甚至尝试删除[self willMoveToParentViewController:self.parentViewController][self removeFromParentViewController]只是为了查看是否有任何改变,但只要[self.view removeFromSuperview]存在,视图就会立即消失。

1 个答案:

答案 0 :(得分:0)

在Parent类中添加

- (void) moveToAddJobVC:(NSString*)storyboardId {

    ChildViewController *destVC = [self.storyboard instantiateViewControllerWithIdentifier:storyboardId];
    destVC.completionHandler = ^(NSString* string) {
        NSLog(@"Returned strng.....%@",string);
        //[self viewWillAppear:YES];
    };
    [destVC.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self addChildViewController:destVC];
    [self.view addSubview:destVC.view];
    [destVC didMoveToParentViewController:self];

    [UIView animateWithDuration:0.3 animations:^{
        [destVC.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    }];

}

在小孩VC写

@property (nonatomic,copy) void (^completionHandler)(NSString *string);

- (IBAction)dismissBtnAction:(id)sender{

    if (self.completionHandler)
    {
        self.completionHandler(@"Return data");
    }
    [self removeFromParentViewController];
    [self.view removeFromSuperview];
}