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);
//設置動畫錨點后,視圖坐標會發(fā)生變化,為了與錨點設置前顯示一致,可以往反方向平移對應的距離
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, 0, self.indicatorView.frame.size.height*0.5);
self.indicatorView.transform = transform;
}
//若未執(zhí)行動畫,且節(jié)拍器開始播放,則開啟動畫
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;
// 動畫結束時是否執(zhí)行逆動畫
rotationAnimation.autoreverses = YES;
rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.removedOnCompletion = NO;
[self.indicatorView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
//若動畫在執(zhí)行,且節(jié)拍器停止,則關閉動畫
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;
});
}