speed 默认为1,如果动画时间为3秒,而想要将动画时间改为1.5秒,则speed 为之前的2倍
- (void)runOrStopRotateAnimation:(BOOL)run {
if (!self.anchorConfiged) {
self.anchorConfiged = YES;
//先设置动画锚点
self.indicatorView.layer.anchorPoint = CGPointMake(0.5, 1);
//设置动画锚点后,视图坐标会发生变化,为了与锚点设置前显示一致,可以往反方向平移对应的距离
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, 0, self.indicatorView.frame.size.height*0.5);
self.indicatorView.transform = transform;
}
//若未执行动画,且节拍器开始播放,则开启动画
if (run && !self.animationRunning) {
[self.indicatorView.layer removeAllAnimations];
self.animationRunning = YES;
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
// 旋转角度
rotationAnimation.fromValue = [NSNumber numberWithFloat:M_PI*-0.15];
rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI*0.15];
rotationAnimation.duration = MetronomeDurationPerBPM;
rotationAnimation.repeatCount = HUGE_VALF;
// 动画结束时是否执行逆动画
rotationAnimation.autoreverses = YES;
rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.removedOnCompletion = NO;
[self.indicatorView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
//若动画在执行,且节拍器停止,则关闭动画
else if (!run && self.animationRunning) {
self.animationRunning = NO;
[self.indicatorView.layer removeAllAnimations];
}
}
- (void)changeSilderAnimationSpeed:(float)speed {
safe_dispatch_main_async(^{
self.indicatorView.layer.timeOffset = [self.indicatorView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
self.indicatorView.layer.beginTime = CACurrentMediaTime();
self.indicatorView.layer.speed = speed;
});
}