如何在后台清除应用程序的所有子视图

Posted

技术标签:

【中文标题】如何在后台清除应用程序的所有子视图【英文标题】:how do I clear all subviews with an app goes in the background 【发布时间】:2013-01-24 03:58:35 【问题描述】:

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

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

【问题讨论】:

【参考方案1】:

你需要在appdelegate的did enter后台方法-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
        );
    

希望这会有所帮助。

【讨论】:

我会看看这个。谢谢。

以上是关于如何在后台清除应用程序的所有子视图的主要内容,如果未能解决你的问题,请参考以下文章

当应用程序移动到后台时,android是不是会清除视图中的数据?

清除子视图时视图混乱

如何管理添加到 UIView 中的子视图?

Android onClickListener 不会为子视图触发

如何在expandablelistview中折叠组视图上方和下方的所有子视图?

Xcode,删除子视图