Ios 如何在后台使用应用程序清除所有子视图
我使用下面的代码,我从斯坦福大学的一个讲座中借用了这个代码,把一个子视图(一个数字)放上去,然后“旋转”这个视图。除了当应用程序在屏幕上显示一个数字时进入后台外,一切正常。当它回来时,号码被冻结了。我想当它进入背景时,我需要清除所有的子视图,但我不知道该怎么做。有什么建议吗? 这是我从斯坦福讲座中“借用”的代码Ios 如何在后台使用应用程序清除所有子视图,ios,view,background-process,Ios,View,Background Process,我使用下面的代码,我从斯坦福大学的一个讲座中借用了这个代码,把一个子视图(一个数字)放上去,然后“旋转”这个视图。除了当应用程序在屏幕上显示一个数字时进入后台外,一切正常。当它回来时,号码被冻结了。我想当它进入背景时,我需要清除所有的子视图,但我不知道该怎么做。有什么建议吗? 这是我从斯坦福讲座中“借用”的代码 - (void)drain { for (UIView *view in self.firstView.subviews) { CGAffineTransform
- (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];
}];
}
}];
}
}];
}
}
}
您需要在app delegate的did enter background方法-applicationidentinterbackground中执行这段代码:
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
});
}
}
希望这能有所帮助。我来看看。谢谢。