[英]pausing and resuming core animation when app enters and exits background
我有一個簡單的CABasicAnimation連接成無限動畫(我有一個輪子,我一直在旋轉)。 這是我設置顯式動畫的方法:
-(void)perform360rotation:(UIImageView*) imageView {
CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
// to rotate around a specific axis, specify it in the KeyPath parameter like below
//CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
[anim setDuration:self.speed]; // Animation duration
[anim setAutoreverses:NO];
[anim setRepeatCount:HUGE_VALF]; // Perfrom animation large number of times
[anim setFromValue:[NSNumber numberWithDouble:0.0f]];
[anim setToValue:[NSNumber numberWithDouble:(M_PI * 2.0f)]];
[[imageView layer] addAnimation:anim forKey:@"wheeloRotation"];
}
我從viewWillAppear方法中調用了這個animtion方法。 當應用程序進入后台然后重新出現時,動畫將不再起作用。 谷歌搜索時,我從Apple那里得到了這個 。 一切都很好,我在同一個viewcontroller.m文件中實現了Apple的這樣的建議,該文件具有用於旋轉視圖的perform360rotation方法。
-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:[self.wheelView layer]];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
NSLog(@"pauseLayer:paused time = %f",pausedTime);
}
-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer timeOffset];
NSLog(@"resumeLayer:paused time = %f",pausedTime);
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:[self.wheelView layer]] - pausedTime;
layer.beginTime = timeSincePause;
}
再次,在谷歌搜索,我看到普遍的共識是我從AppDelegate調用暫停和恢復方法。 所以我這樣做了(lVC是我在本期問題開頭提到過的viewcontroller.m類。從viewWillAppear和viewWillDisappear方法中調用pauseLayer和resumeLayer方法):
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[lVC viewWillDisappear:YES];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[lVC viewWillAppear:YES];
}
沒有骰子。 當應用重新進入前景時,動畫仍然無法恢復。 有什么我做錯了嗎?
調用viewWillAppear
可能會導致其他副作用,並且通常不是啟動動畫的好地方,嘗試偵聽UIApplicationWillEnterForegroundNotification
並在處理程序中添加resumeLayer:
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.