2015-12-01 134 views
2

我需要不斷旋轉UIImageView。要做到這一點,我發現這個代碼:用CGAffineTransform動畫混合CABasicAnimation

if ([self.image.layer animationForKey:@"SpinAnimation"] == nil) { 

     CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
     animation.fromValue = [NSNumber numberWithFloat:0.0f]; 
     animation.toValue = [NSNumber numberWithFloat: 2 * M_PI]; 
     animation.duration = 90.0f; 
     animation.repeatCount = INFINITY; 
     [self.image.layer addAnimation:animation forKey:@"SpinAnimation"]; 
    } 

然後,我需要在圖像視圖的變換翻譯,製作動畫。如果我這樣做:

CGAffineTransform transform = self.image.transform; 
transform = CGAffineTransformTranslate(transform, 0, 350); 
[UIView animateWithDuration:1.0f animations:^{ 

     [self.view layoutIfNeeded]; 
     self.image.transform = transform; 
    } completion:^(BOOL finished) { 

     self.isMiddleViewOpened = YES; 
    }]; 

在進行動畫,圖像漂浮各地視圖抵達翻譯的終點之前。 感謝☺️

編輯 我有問題,因爲在我的第二個轉換我只編輯Y值,但圖像並不僅僅在Y軸移動。如果您嘗試使用此代碼,您將看到問題

+0

什麼是變形?它是否旋轉圖像? –

+0

你的代碼有什麼問題?你有什麼問題嗎? –

+0

我編輯我的帖子 –

回答

2

無法混合使用這兩種動畫,您可以使用Core動畫製作所有動畫素材。

我在我的閃屏中使用這個來旋轉四個圖像塊,並最終將它們合併爲一個。

動畫會是這樣的:

enter image description here

代碼爲一個圖像塊:左上塊,我把它叫做00塊

- (void)addSplashScreenAnimationWithCompletion:(void (^)(BOOL finished))completionBlock 
{ 
    [self addSplashScreenAnimationWithBeginTime:0 andFillMode:kCAFillModeBoth andRemoveOnCompletion:NO completion:completionBlock]; 
} 

- (void)addSplashScreenAnimationWithBeginTime:(CFTimeInterval)beginTime andFillMode:(NSString *)fillMode andRemoveOnCompletion:(BOOL)removedOnCompletion completion:(void (^)(BOOL finished))completionBlock 
{ 
    CAMediaTimingFunction *linearTiming = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 

    if (completionBlock) 
    { 
     CABasicAnimation *representativeAnimation = [CABasicAnimation animationWithKeyPath:@"not.a.real.key"]; 
     representativeAnimation.duration = 8.500; //your duration 
     representativeAnimation.delegate = self; 
     [self.layer addAnimation:representativeAnimation forKey:@"SplashScreen"]; 

    } 

    CAKeyframeAnimation *_00RotationAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"]; 
    _00RotationAnimation.duration = 8.500; 
    _00RotationAnimation.values = @[@(0.000), @(18.829), @(18.829)]; 
    _00RotationAnimation.keyTimes = @[@(0.000), @(0.353), @(1.000)]; 
    _00RotationAnimation.timingFunctions = @[linearTiming, linearTiming]; 
    _00RotationAnimation.beginTime = beginTime; 
    _00RotationAnimation.fillMode = fillMode; 
    _00RotationAnimation.removedOnCompletion = removedOnCompletion; 


    CAKeyframeAnimation *_00OpacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"]; 
    _00OpacityAnimation.duration = 8.500; 
    _00OpacityAnimation.values = @[@(0.000), @(0.497), @(0.553), @(0.759), @(0.921), @(1.000), @(0.642), @(0.341), @(0.000), @(0.000)]; 
    _00OpacityAnimation.keyTimes = @[@(0.000), @(0.059), @(0.118), @(0.176), @(0.235), @(0.353), @(0.471), @(0.647), @(0.765), @(1.000)]; 
    _00OpacityAnimation.timingFunctions = @[linearTiming, linearTiming, linearTiming, linearTiming, linearTiming, linearTiming, linearTiming, linearTiming, linearTiming]; 
    _00OpacityAnimation.beginTime = beginTime; 
    _00OpacityAnimation.fillMode = fillMode; 
    _00OpacityAnimation.removedOnCompletion = removedOnCompletion; 


    CAKeyframeAnimation *_00ScaleXAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"]; 
    _00ScaleXAnimation.duration = 8.500; 
    _00ScaleXAnimation.values = @[@(0.600), @(0.600), @(1.187), @(1.187)]; 
    _00ScaleXAnimation.keyTimes = @[@(0.000), @(0.353), @(0.765), @(1.000)]; 
    _00ScaleXAnimation.timingFunctions = @[linearTiming, linearTiming, linearTiming]; 
    _00ScaleXAnimation.beginTime = beginTime; 
    _00ScaleXAnimation.fillMode = fillMode; 
    _00ScaleXAnimation.removedOnCompletion = removedOnCompletion; 

    CAKeyframeAnimation *_00ScaleYAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"]; 
    _00ScaleYAnimation.duration = 8.500; 
    _00ScaleYAnimation.values = @[@(0.688), @(0.688), @(1.359), @(1.359)]; 
    _00ScaleYAnimation.keyTimes = @[@(0.000), @(0.353), @(0.765), @(1.000)]; 
    _00ScaleYAnimation.timingFunctions = @[linearTiming, linearTiming, linearTiming]; 
    _00ScaleYAnimation.beginTime = beginTime; 
    _00ScaleYAnimation.fillMode = fillMode; 
    _00ScaleYAnimation.removedOnCompletion = removedOnCompletion; 


    CAKeyframeAnimation *_00TranslationXAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"]; 
    _00TranslationXAnimation.duration = 8.500; 
    _00TranslationXAnimation.values = @[@(0.000), @(107.423), @(211.936), @(211.936)]; 
    _00TranslationXAnimation.keyTimes = @[@(0.000), @(0.353), @(0.765), @(1.000)]; 
    _00TranslationXAnimation.timingFunctions = @[linearTiming, linearTiming, linearTiming]; 
    _00TranslationXAnimation.beginTime = beginTime; 
    _00TranslationXAnimation.fillMode = fillMode; 
    _00TranslationXAnimation.removedOnCompletion = removedOnCompletion; 


    CAKeyframeAnimation *_00TranslationYAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"]; 
    _00TranslationYAnimation.duration = 8.500; 
    _00TranslationYAnimation.values = @[@(0.000), @(390.498), @(476.237), @(476.237)]; 
    _00TranslationYAnimation.keyTimes = @[@(0.000), @(0.353), @(0.765), @(1.000)]; 
    _00TranslationYAnimation.timingFunctions = @[linearTiming, linearTiming, linearTiming]; 
    _00TranslationYAnimation.beginTime = beginTime; 
    _00TranslationYAnimation.fillMode = fillMode; 
    _00TranslationYAnimation.removedOnCompletion = removedOnCompletion; 


} 

上述方法中包含系列動畫如旋轉,不透明changin,沿x和y軸縮放,沿x和y軸平移。 您需要繼承UIImageViewUIView並將動畫放在那裏。

您可以撥打上面的方法,如:

//Here _splashView can be UIView or UIImageView 

[_splashView addSplashScreenAnimationWithCompletion:^(BOOL finished) { 

     //Do your stuff after animation is completed 
}]; 

可以使用repeatCountCAKeyFrameAnimation過了,你應該給HUGE_VALF獲得無限次的旋轉。

+0

那麼,關鍵是使用CAKeyframeAnimation?但是,我的圖像必須始終旋轉,但在我的視圖打開時在其y軸上進行翻譯,並在視圖關閉時返回到其位置。我怎樣才能實現這個?我有點困惑 –

+0

根據我的要求,我旋轉了一些圖像的圖像,你可以永遠旋轉你的形象!所以當你打開你的視圖時,圖像必須通過旋轉移動到某個位置? –

+0

您也可以使用repeatCount for CAKeyFrameAnimation,並且您應該給HUGE_VALF以獲得無限時間旋轉。 –

相關問題