스프라이트 키트를 기반으로하는 앱에서 작업하고 있습니다. 앱이 백그라운드에서 실행되는 동안 모든 작업이 일시 중지된다는 것을 알고 있습니다. 그러나, 나는 페이딩 컬러 일을하고있다. 그래서 나는 앱이 배경에있는 동안 시간을 계산하고 페이딩 동작 인 SKAction을 점프해서 시간이 지나면 색이 바뀌지 않는 것처럼 보이지 않도록한다. 앱이 백그라운드에 있습니다. 그래서 누군가 그렇게하는 방법에 대한 자세한 연습을 해 줄 수 있습니까? 정말 고마워!!백그라운드에서 시간을 계산하고 SKAction이 시간 이후 점프하는 방법
답변
rmaddy 님의 제안을 바탕으로 편집되었습니다. 앱이 활성화 될 때 열리는보기 컨트롤러에서 앱이 전경으로 들어가거나 나올 때 알림을 받으려면 UIApplicationDidBecomeActiveNotification and UIApplicationWillResignActiveNotification
에 직접 구독 할 수 있습니다.
@property(nonatomic) NSDate *enterBackground;
@property (nonatomic) NSTimeInterval enterForeground;
-(void)viewDidLoad{
[super viewDidLoad];
[self subscribeToNotifications];
}
-(void)viewWillDisappear:(BOOL)animated{
[self unsubscribeFromNotifications];
}
-(void)subscribeToNotifications{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEntersBackground) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEntersForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
-(void)unsubscribeFromNotifications{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
}
이제 ViewController가 앱이 포그 라운드를 시작하고 종료하는 시점을 알고 있으므로 타이머를 시작하고 중지하는 메소드를 추가하십시오. 이것은 단지 시각적 인 효과 인 경우
-(void)appEntersBackground{
self. enterBackground = [NSDate date];
}
-(void)appEntersForeground{
self.enterForeground = [[NSDate date]timeIntervalSinceDate:self.enterBackground];
NSLog(@"%f",self.enterForeground);
}
배경에 소요 된 시간은 배경에서 반환 할 때, 그냥 무작위 색상을 설정 self.enterForeground
당신의 이것에 대한 자신의 알림? 앱 대리자 메소드에서 코드를 제거하고 뷰 컨트롤러에서 표준 ('UIApplicationDidBecomeActiveNotification' 및'UIApplicationWillResignActiveNotification') 알림에 간단히 가입하십시오. BTW - 문자열 상수가 아닌 상수입니다. – rmaddy
@rmaddy 아주 좋은 점, 왜 그렇게 복잡하게하는지 모르겠다! –
정말 고마워 !! 나는 백그라운드에서 시간을 계산했다. 하지만 앱이 활성화 된 후에 상태를 점프하는 방법에 대한 질문이 있습니다. 나는 무언가의 색을 퇴색시키는 약점을 피하고있다. 조언 좀 해줄 수 있니? –
에 액세스 할 수 있습니다 - 아무도 알 수 없습니다, 당신이 정의하는 이유) – LearnCocos2D