UIView animateWithDuration忽略持续时间

时间:2016-12-08 16:08:14

标签: ios objective-c animation uiview

我的目标很简单,我之前已经实现了这一点,这就是我在这里提出问题的原因。

我正在尝试动画子视图。很简单。在动画结束后,视图最终到达了我想要的位置。唯一的问题是: 无论我做什么,动画持续时间都被完全忽略。它会立即发生。

- (void)callToDismissView:(UIView *)view {

     [self.view addSubview:view];

     [self dismissViewControllerAnimated:NO completion:nil];

     [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){

         CGRect frame = self.view.frame;
         frame.origin.x = frame.size.width;
         view.frame = frame;

     } completion:^(BOOL finished){

         [view removeFromSuperview];
     }];
 }

现在我尝试了不同的帧和持续时间来查看视图是否完全动画,它总是在我需要它的地方结束。只是立即...

修改

上面的代码由被解雇的视图控制器中的委托调用,如下所示:

- (void)dismissSelf {

    if (self.delegate && [self.delegate respondsToSelector:@selector(callToDismissScan)]) {

        [self.delegate callToDismissScan];
    }
}

该方法本身是由通知触发的。

我必须承认,在我不完全理解的两个控制器之间的过渡期间发生了一些事情。 (我非常想了解他们......)

3 个答案:

答案 0 :(得分:1)

你可以试试 [self.view layoutIfNeeded];

像这样

- (void)callToDismissView:(UIView *)view {

 [self.view addSubview:view];

 [self dismissViewControllerAnimated:NO completion:nil];
 [self.view layoutIfNeeded];
 [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){

     CGRect frame = self.view.frame;
     frame.origin.x = frame.size.width;
     view.frame = frame;
     [self.view layoutIfNeeded];

 } completion:^(BOOL finished){

     [view removeFromSuperview];
 }];
 }

答案 1 :(得分:0)

也许你需要这样尝试。

- (void)callToDismissView:(UIView *)view {
    [self.view addSubview:view];
    CGRect frame = self.view.frame;
    frame.origin.x = frame.size.width;
    [UIView animateWithDuration:0.3 delay:0.3 
                        options:UIViewAnimationOptionCurveEaseInOut animations:^(void){
                            view.frame = frame;
                        }
                     completion:^(BOOL finished){
                            [view removeFromSuperview];
                            //[self dismissViewControllerAnimated:NO completion:nil];//uncomment if need.
                     }];
}

答案 2 :(得分:0)

似乎没有办法在地狱中我可以为呈现视图控制器中的过渡设置动画,因此解决问题的最简单方法是在呈现视图控制器被解除之前设置转换动画。 我这样做的方法是将委托转换为视图控制器。然后很容易将它的视图添加为我呈现的视图控制器的子视图,并将其设置为动画:

- (void)dismissSelf {

    UIView *dummyView = [ScreenCapper captureScreenIn: self];
    UIViewController *presentingViewController = (UIViewController *)self.delegate;
    [self.view.window addSubview: presentingViewController.view];
    [self.view.window addSubview: dummyView];

    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){

        dummyView.frame = CGRectMake(dummyView.frame.size.width, dummyView.frame.origin.y, dummyView.frame.size.width, dummyView.frame.size.height);

    } completion:^(BOOL finished){
        if (self.delegate && [self.delegate respondsToSelector:@selector(callToDismissView)]) {

            [self.delegate callToDismissView];
        }
    }];
}

在呈现视图控制器中,剩下要做的就是在没有动画的情况下关闭呈现的视图控制器。

- (void)callToDismissView {

    [self dismissViewControllerAnimated:NO completion:nil];
}