2013-03-12 89 views
0

我有一個UIImageView與一張臉的圖像,然後我應用旋轉變換動畫到該UIImageView。UIimageView設置變換後丟失幀的原點位置

self.transform = CGAffineTransformRotate(self.transform, M_PI/16); 

[UIView animateWithDuration:0.5 
        delay:0.0 
       options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse 
      animations:^{ 

       self.transform = CGAffineTransformRotate(self.transform, -1 * (M_PI/8)); 

      } 
      completion:^(BOOL completed) {}]; 

然後我有另一個相同的臉部圖像在y軸上上下移動的動畫。

[UIView animateWithDuration:0.2 
         delay:0.0 
        options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse 
       animations:^{ 

        self.transform = CGAffineTransformMakeTranslation(0, 10); 
       } 
       completion:^(BOOL completed){}]; 

當我執行第一個動畫後,我需要更改UIImageView的位置,然後開始第二個動畫。

我有一個stopAnimation方法,可以刪除動畫,重置轉換,並設置UIImageView的位置。

-(void)stopAnimation:(NSInteger)currentAnimation{ 

    [self.layer removeAllAnimations]; 

    [self setTransform:CGAffineTransformIdentity]; 
    [self setFrame:initFrame]; 

    CGPoint position = [self getNextPosition:currentAnimation]; 
    //[self setTransform:CGAffineTransformMakeTranslation(0, 0)]; 
    CGRect frame = self.frame; 
    frame.origin.x = position.x; 
    frame.origin.y = position.y; 
    self.frame = frame; 
} 

-(CGPoint)getNextPosition:(NSInteger)currentAnimation{ 

    CGPoint position = [[positions objectAtIndex:(currentAnimation-1)] CGPointValue]; 
    NSLog(@"Current animation:%u %f:%f", currentAnimation,position.x,position.y); 
    return position; 
} 

如果我執行的位置,而動畫的UIImageView的是在屏幕的正確位置,但如果我與動畫運行時,動畫工作好但UIImageView的位置是錯誤的改變。

我試過相同的設置UIImageView的中心,但我有同樣的問題。

回答

0

嘗試在第一個動畫的持續時間內抵消第二個動畫。

[UIView animateWithDuration:0.2 
         delay:0.51 //delay by the duration of the first animation 
        options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse 
       animations:^{ 
        self.transform = CGAffineTransformConcat(self.transform, CGAffineTransformMakeTranslation(0, 10)); 
       } 
       completion:^(BOOL completed){}]; 

您還可以嘗試將第二個動畫塊放入第一個動畫的完成塊中。

+0

但是,我有控制動畫運行的時間,也執行動畫後我removeAllAnimations。 – Tony 2013-03-12 18:45:30

+0

stopAnimation在哪裏被調用?一些常規筆記,因爲我不知道它是如何工作的:UIView動畫是異步的,所以你的動畫塊下的代碼將在動畫完成之前被調用。另外,將轉換設置爲平移將消除旋轉。 – architectpianist 2013-03-12 18:55:50

+0

該應用程序從第一個動畫開始,然後當用戶敲擊stopAnimation時被調用,然後調用第二個動畫。 – Tony 2013-03-12 21:25:17