简体   繁体   English

如何在应用程序中清除所有子视图

[英]how do I clear all subviews with an app goes in the background

I using the following code that I borrowed from one of the Stanford lectures put a subview (a number) up then "spin" the view down the drain. 我使用从斯坦福大学的一次讲座中借来的以下代码将子视图(数字)向上,然后将视图“旋转”到下层。 It all works fine except when the app goes into the background when a number is on the screen. 一切正常,除非当屏幕上显示数字时应用程序进入后台。 when it comes back the number is frozen. 当它回来时,数字被冻结。 I think I need to clear all of the subviews when it goes into the background, but I'm stumped how to do that. 我认为当它进入后台时,我需要清除所有子视图,但是我很困惑如何做到这一点。 Any suggestions? 有什么建议么? Here's the code I "borrowed" from the Stanford lecture. 这是我从斯坦福大学讲座中“借用”的代码。

- (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];
                            }];
                        }
                    }];
                }
            }];
        }
    }
}

You need to do this piece of code in app delegate's did enter background method - applicationDidEnterBackground: 您需要在应用程序委托的进入背景方法-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
        });
    }
}

Hope this helps. 希望这可以帮助。

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

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