App直接退出回到主界面(非閃退,非崩潰)
之前在群里看到有人問(wèn),不使用Home鍵,在應(yīng)用中怎么讓?xiě)?yīng)用退出回到桌面,用閃退方法退出很容易但畢竟體驗(yàn)不好,之后看了幾篇博客說(shuō)添加個(gè)動(dòng)畫(huà)調(diào)用exit函數(shù),然后試了一下效果是比閃退好,如果有更好的方法歡迎指教....
方法一、
- (void) ExitApp {
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
UIWindow *window = app.window;
[UIView animateWithDuration:0.5f animations:^{
window.alpha = 0.2;
self.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height*0.7, 0, 0);
} completion:^(BOOL finished) {
exit(0);
}];
}
方法二、
- (void) ExitApp {
[UIView beginAnimations:@"exitApplication" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view.window cache:NO];
[UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
[UIView commitAnimations];
}
/**
* 退出APP代碼
* @param animationID animationID
*/
- (void)animationFinished:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
if ([animationID compare:@"exitApplication"] == 0) {
exit(0);
}
}