core animation使用旋轉角度,搖擺效果

今天看某位同學問時鐘效果怎麼做,我首先想到的就是view的屬性transform裏面有旋轉角度的方法,當然還有其他的方法,只是我感覺這個最簡單(尷尬)。

有兩種方式,一個是定時器控制動畫,一個是利用core animation的重複動畫屬性;

定時器

這個不推薦使用,就是利用UIView的封裝動畫,在以定時器來重複運行動畫代碼,上代碼:
- (void)animation {
    
    [UIView animateWithDuration:1 animations:^{
        _animationView.transform = CGAffineTransformMakeRotation(M_PI * 1 / 4);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 animations:^{
            _animationView.transform = CGAffineTransformMakeRotation(-M_PI * 1 / 4);
        }];
    }];
}

CABasicAnimation

這個使用方法、代碼簡單

- (void)coreAnimationType {
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    animation.fromValue = [NSNumber numberWithFloat:-M_PI / 4];
    animation.toValue = [NSNumber numberWithFloat:M_PI / 4];
    
    animation.autoreverses = YES; //設置YES,代表動畫每次重複執行的效果和上一次的相反
    animation.duration = 1; //一次動畫的完成時間
    animation.repeatCount = NSNotFound; //動畫播放次數 NSNotFound是無限個循環
    
    [_animationView.layer addAnimation:animation forKey:@"animationTransform"];
}

注意:弧度的旋轉利用“transform.rotation”來重建BasicAnimation


還有其他的方式來實現,比如阻尼動畫


源碼地址:http://download.csdn.net/detail/pianzhidenanren/9587840

發佈了42 篇原創文章 · 獲贊 9 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章