CABasicAnimation resets when edge swiping with UINavigationController

徘徊边缘 提交于 2019-12-11 10:11:25

问题


I have the following CABasicAnimation run in my viewDidLoad for a view controller inside a UINavigationController:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.duration = 1;
animation.additive = NO;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.fromValue = [NSNumber numberWithFloat:0];
animation.toValue = [NSNumber numberWithFloat:-1*(((-windBearing+180) * M_PI) / 180)];
[compass.layer addAnimation:animation forKey:@"90rotation"];

When I use the edge swipe gesture to slowly navigate back, the animation resets to the initial state, which is quite jarring. I have my fillMode set and removedOnCompletion set to NO, what am I missing?


回答1:


This whole trick of setting the fillMode and the removedOnCompletion is nonsense - it's bad advice that has worked itself into a lot of answers, but it's totally wrong. The animation is one thing; the actual transform of the compass layer is another. You have applied the animation but you have forgotten to set the transform to match it. Therefore, when the gesture starts and the animation is removed from the layer, the compass is shown at its actual rotation (and the change appears as a jump).




回答2:


Thanks to matt above, I realized I was missing the following line:

compass.transform = CGAffineTransformMakeRotation(-1*(((-windBearing+180) * M_PI) / 180));


来源:https://stackoverflow.com/questions/33597503/cabasicanimation-resets-when-edge-swiping-with-uinavigationcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!