1

我使用从斯坦福讲座中借来的以下代码放置了一个子视图(一个数字),然后将视图“旋转”到下水道。一切正常,除非当屏幕上有数字时应用程序进入后台。当它回来时,数字被冻结。我认为当它进入后台时我需要清除所有子视图,但我很难做到这一点。有什么建议么?这是我从斯坦福讲座“借来”的代码。

- (void)drain
{
    for (UIView *view in self.firstView.subviews) {
        CGAffineTransform transform = view.transform;
        if (CGAffineTransformIsIdentity(transform)) {
            UIViewAnimationOptions options = UIViewAnimationOptionCurveLinear;
            [UIView animateWithDuration:DRAIN_DURATION/3 delay:0 options:options animations:^{
                view.transform = CGAffineTransformRotate(CGAffineTransformScale(transform, 0.7, 0.7), 2*M_PI/3);
            } completion:^(BOOL finished) {
                if (finished) {
                    [UIView animateWithDuration:DRAIN_DURATION/3 delay:0 options:options animations:^{
                        view.transform = CGAffineTransformRotate(CGAffineTransformScale(transform, 0.4, 0.4), -2*M_PI/3);
                    } completion:^(BOOL finished) {
                        if (finished) {
                            [UIView animateWithDuration:DRAIN_DURATION/3 delay:0 options:options animations:^{
                                view.transform = CGAffineTransformScale(transform, 0.1, 0.1);
                            } completion:^(BOOL finished) {
                                if (finished) [view removeFromSuperview];
                            }];
                        }
                    }];
                }
            }];
        }
    }
}
4

1 回答 1

0

您需要在应用程序委托的确实进入后台方法中执行这段代码 - applicationDidEnterBackground:

if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if your iOS version supports multitasking I.E iOS 4
    if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
        UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance
        __block UIBackgroundTaskIdentifier background_task; //Create a task object
        background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {

        [application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
            background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
            //System will be shutting down the app at any point in time now
        }];
        //Background tasks require you to use asyncrous tasks
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //Perform your tasks that your application requires


 // Call your drain method here or raise a notification if this drain is not in the app delegate

            NSLog(@"\n\nRunning in the background!\n\n");
            [application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
            background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
        });
    }
}

希望这可以帮助。

于 2013-01-24T04:17:15.177 回答