改變動畫速率

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;
    });
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容