简体   繁体   English

完成时的UIView动画

[英]UIView animation on completion

I have the following code, which works perfectly fine. 我有以下代码,可以很好地工作。 What it does is just moves a dynamically created image from one point to another. 它所做的只是将动态创建的图像从一个点移动到另一个点。

    [UIView animateWithDuration:duration
                      delay:0
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     [UIView setAnimationBeginsFromCurrentState:YES];
                     CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
                     image.transform = transform;
                 }
                 completion:^(BOOL finished){

                     if (finished) {
                         NSLog(@"finished animation in true");
                         [arrOfTempImages removeObject:image];
                         image.image = nil;
                         // and do some other stuff here
                     } else {
                         NSLog(@"finished animation in false");
                     }
                 }];

However, sometimes I need to change the viewController and so I pause the animation using this method found in apple's documentation when the view is about to disappear: 但是,有时我需要更改viewController,因此当视图即将消失时,我使用苹果文档中提供的此方法暂停动画:

-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}

That also works perfectly fine. 那也很好用。 When I go back to the old VC, I resume my animation using the method : 当我回到旧的VC时,我使用以下方法恢复动画:

-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}

Which again works fine.... But the problem is that the code "NSLog(@"finished animation in false"); " is executing when I change the VC and when I go back to the original VC and wait for the image to arrive at the end point, neither this code nor "NSLog(@"finished animation in true");" 再次可以正常工作。...但是问题是,当我更换VC并返回到原始VC并等待图像返回时,代码“ NSLog(@“完成的动画为假”);“正在执行到达终点,此代码或“ NSLog(@“完成的动画为真”)都不会;“ is executed. 被执行。 That means the image, when it arrives at the end point, doesn't get pointed to nil and remains on screen. 这意味着图像到达终点时不会指向nil,而是保留在屏幕上。

What can I do? 我能做什么? I want to perform some code in "completion:" but that part of the code isn't called anymore upon re-entry of the original VC 我想在“ completion:”中执行一些代码,但是在重新输入原始VC时不再调用该部分代码

According to Apple's docs , using [UIView setAnimationBeginsFromCurrentState:YES] is discouraged in iOS 4+. 根据Apple的文档 ,不建议在iOS 4+中使用[UIView setAnimationBeginsFromCurrentState:YES]

Instead, try changing your options param from UIViewAnimationOptionCurveEaseOut to UIViewAnimationOptionBeginFromCurrentState and it should call the completion block. 相反,尝试将选项参数从UIViewAnimationOptionCurveEaseOut更改为UIViewAnimationOptionBeginFromCurrentState ,它应该调用完成块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM